我正在嘗試在 javascript 中創建一個函式,以匹配來自 API 的鍵和物件陣列。匹配基于來自 API 的 2 個引數。Akey
它是一個字串和一個具有鍵和值的物件陣列。這個匹配的輸出應該是一個新的物件陣列,其中傳遞的鍵與作為引數傳遞的物件陣列的鍵匹配。
我準備了一個我正在嘗試實作但沒有作業的樣本,因為我不知道如何正確地做到這一點,下面將是關于它的詳細資訊
const criteriaJson = [{
"age": {
"min": "18",
"max": "65"
},
"pain": {
"min": "5"
},
"disease": {
"valid": [
"MDD",
"PTSD"
],
"invalid": [
"None"
]
},
"medicines": "true",
"bmi": {
"min": "25",
"max": "100"
},
"weight": "90",
"gender": [
"1",
"2",
"3"
],
"pressure": "100",
"smoker": "20",
"zip": "^w s{1}w $"
},
{
"age": {
"min": "16",
"max": "18"
},
"pain": {
"max": "10"
},
"bmi": {
"max": "85"
},
"disease": {
"valid": [
"none"
],
"invalid": [
"PTT",
"ADT",
"OLL"
]
},
"weight": "70",
"gender": [
"1"
],
"pressure": "10"
}
]
const question = {
key: "medicines"
}
// *Matching and extracting the right criteria data for the provided question key
// *inside the criteria object
// *returns an object with the right criteria
// *doing it for every group set of rules
function questionCriteriaKeyMatch(criteriaJson, question) {
criteriaJson.map((criteria) => {
return Object.keys(criteria)
.filter((key) => {
return key === question.key;
})
.reduce((cur, key) => {
return Object.assign(cur, {
criteria: criteria[key],
});
}, {});
});
}
console.log(questionCriteriaKeyMatch(criteriaJson, question));
為了解釋我們得到了什么資料,criteriaJson
這個資料是一個物件陣列,其中包含一組規則,這些規則分為同一專案的物件,但參考不同的組。
物件 1 是標準 1,物件 2 是標準 2,以便在這種特定情況下變得簡單。
[{
"age": {
"min": "18",
"max": "65"
},
"pain": {
"min": "5"
},
"disease": {
"valid": [
"MDD",
"PTSD"
],
"invalid": [
"None"
]
},
"medicines": "true",
"bmi": {
"min": "25",
"max": "100"
},
"weight": "90",
"gender": [
"1",
"2",
"3"
],
"pressure": "100",
"smoker": "20",
"zip": "^w s{1}w $"
},
{
"age": {
"min": "16",
"max": "18"
},
"pain": {
"max": "10"
},
"bmi": {
"max": "85"
},
"disease": {
"valid": [
"none"
],
"invalid": [
"PTT",
"ADT",
"OLL"
]
},
"weight": "70",
"gender": [
"1"
],
"pressure": "10"
}
]
但是,該資料的案例基本上是 3
- 物件為空,因為在這種情況下,API 中不存在任何條件,與不存在的鍵匹配將輸出 null
- 陣列只有一個物件,所以匹配需要在這個唯一的物件上完成,如果鍵不匹配輸出 null
- 在這種情況下,我們有一個包含多個物件的陣列,我們需要為每個物件匹配鍵,如果不匹配,則輸出為空。
從我們收到一個空物件陣列的簡單案例開始
const obj = [];
const key = 'someKey';
// Output as there is no matching
[null]
第二種情況的示例,當我們在陣列中有一個物件時
const obj = [{
"age": {
"min": "18",
"max": "65"
},
"pain": {
"min": "5"
},
"disease": {
"valid": [
"MDD",
"PTSD"
],
"invalid": [
"None"
]
},
}]
// Output matching key === age -> I'm open to suggestions how this output should be for // one obj only in the array
[{
criteria: { min:"18", max:"65" }
}]
// key === pain
[{
criteria: "5"
}]
// key === disease
[{
criteria: {valid: ["MDD","PTSD"], invalid: ["None"] }
}]
// key === notExistingKey means the key we are passing to match doesn't have a match so // we output null in this case
[{ null }]
從上面的例子中,我們輸出匹配的值與作為引數傳遞的鍵,當沒有匹配時為空
最后一種情況可能更復雜,因為我們有一個條件物件陣列,這應該適用于陣列長度 > 1。
使用與顯示預期輸出的片段中相同的資料
const criteriaJson = [{
"age": {
"min": "18",
"max": "65"
},
"pain": {
"min": "5"
},
"disease": {
"valid": [
"MDD",
"PTSD"
],
"invalid": [
"None"
]
},
"medicines": "true",
"bmi": {
"min": "25",
"max": "100"
},
"weight": "90",
"gender": [
"1",
"2",
"3"
],
"pressure": "100",
"smoker": "20",
"zip": "^w s{1}w $"
},
{
"age": {
"min": "16",
"max": "18"
},
"pain": {
"max": "10"
},
"bmi": {
"max": "85"
},
"disease": {
"valid": [
"none"
],
"invalid": [
"PTT",
"ADT",
"OLL"
]
},
"weight": "70",
"gender": [
"1"
],
"pressure": "10"
}
]
const key = 'medicines'
// the output should be a new array of objects as we need to compare each object to find // the key match and output every single value
// key === medicines
[
{ criteria: 'true' }, -> we have a match in the first obj
{ criteria: null }, -> we have no match in the second obj
]
// key === age
[
{ criteria: { min: "18", max: "65" }}, -> we have a match in the first obj
{ criteria: { min: "16", max: "18" }}}, -> we have a match in the second obj
]
// For the rest of the keys should follow the same logic and with more objects, we have more
// objects in the output we have
我正在添加criteria
,因為我需要在第二階段能夠訪問該 obj 并提取值以供進一步使用。由于密鑰并不總是與客戶端中 API 集的字串相同,因此我需要一種通過分配硬編碼密鑰來促進訪問新物件的方法。這就是為什么當匹配時我只需要值和鍵之類的標準,所以我可以毫無問題地訪問這些值
我從 reduce 方法開始,因為我認為這可能是一種方式,但最后,我對新的解決方案持開放態度,因為無法弄清楚如何實作我的目標。
重要的一點是我不能使用 for 回圈,因為我有嚴格的代碼風格規則,不能改變。
如果有任何關于特定問題的評論或請求將嘗試更好地解釋或用更相關的資訊更新我的問題。
uj5u.com熱心網友回復:
似乎您只想將陣列映射到僅包含值的新陣列。如果鍵不存在,則只需將條件值設定為 null,否則將條件設定為屬性值。
const criteriaJson = [{
"age": {
"min": "18",
"max": "65"
},
"pain": {
"min": "5"
},
"disease": {
"valid": [
"MDD",
"PTSD"
],
"invalid": [
"None"
]
},
"medicines": "true",
"bmi": {
"min": "25",
"max": "100"
},
"weight": "90",
"gender": [
"1",
"2",
"3"
],
"pressure": "100",
"smoker": "20",
"zip": "^w s{1}w $"
},
{
"age": {
"min": "16",
"max": "18"
},
"pain": {
"max": "10"
},
"bmi": {
"max": "85"
},
"disease": {
"valid": [
"none"
],
"invalid": [
"PTT",
"ADT",
"OLL"
]
},
"weight": "70",
"gender": [
"1"
],
"pressure": "10"
}
]
function getCriteria (key, data) {
if (!data?.length) return [null]
return data.map(item => ({ criteria: item[key] || null }));
}
console.log('age', getCriteria('age', criteriaJson));
console.log('medicines', getCriteria('medicines', criteriaJson));
console.log('pressure', getCriteria('pressure', criteriaJson));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/505017.html
標籤:javascript 数组 json