我有以下輸入。它是一個物件陣列,每個物件都有狀態,這也是一個物件陣列。details當狀態 id 與id下面提到的匹配時,我想在狀態物件中附加。IE82175746
const input =
[
{
"country": { "id": 87745195, "action": "Analyze" },
"states": [
{ "id": 83589582, "action": "Verify" },
{ "id": 87335656, "action": "Analyze" }
]
},
{
"country": { "id": 83861166, "action": "Verify" },
"states": [
{ "id": 82175746, "action": "Closed" },
{ "id": 78745158, "action": "Closed" }
]
}
]
const details = { "totalOpenRadars": 1, "totalClosedRadars": 1 }
const id = 82175746
這就是我想要達到的結果。請注意,將 82175746 的 id 與所有狀態 id 進行比較。一旦找到匹配項,上述詳細資訊將如下所示附加到匹配的物件中。
const result =
[
{
"country": { "id": 87745195, "action": "Analyze" },
"states": [
{ "id": 83589582, "action": "Verify" },
{ "id": 87335656, "action": "Analyze" }
]
},
{
"country": { "id": 83861166, "action": "Verify" },
"states": [
{ "id": 82175746, "action": "Closed", "details": { "totalOpenRadars": 1, "totalClosedRadars": 1 } },
{ "id": 78745158, "action": "Closed" }
]
}
]
為了實作這一點,我嘗試了這種方式,但我無法正確獲得結果。有人可以讓我知道我哪里出錯了嗎
const result = input.forEach((element) => {
element.states.forEach((state) => {
if(state.id === id) {
state.details = details
}
});
});
uj5u.com熱心網友回復:
Array.forEach總是回報undefined,所以result永遠都是undefined。如果您查看input操作后,它確實包含您指定的詳細資訊。
或者,如果您打算保持不變,您可以擴展input到一個名為result并執行回圈的新陣列。resultinput
const input =
[
{
"country": { "id": 87745195, "action": "Analyze" },
"states": [
{ "id": 83589582, "action": "Verify" },
{ "id": 87335656, "action": "Analyze" }
]
},
{
"country": { "id": 83861166, "action": "Verify" },
"states": [
{ "id": 82175746, "action": "Closed" },
{ "id": 78745158, "action": "Closed" }
]
}
]
const details = { "totalOpenRadars": 1, "totalClosedRadars": 1 }
const id = 82175746
input.forEach((element) => {
element.states.forEach((state) => {
if(state.id === id) {
state.details = details
}
});
});
console.log(input);
uj5u.com熱心網友回復:
您可以使用Array.prototype.map():
const input = [{"country": { "id": 87745195, "action": "Analyze" },"states": [{ "id": 83589582, "action": "Verify" },{ "id": 87335656, "action": "Analyze" }]},{"country": { "id": 83861166, "action": "Verify" },"states": [{ "id": 82175746, "action": "Closed" },{ "id": 78745158, "action": "Closed" }]}]
const details = { "totalOpenRadars": 1, "totalClosedRadars": 1 }
const id = 82175746
const result = input.map(item => item.states.map(state => ({
...state,
...(state.id === id ? { details } : {})
})))
console.log(result)
uj5u.com熱心網友回復:
這應該做
const appendDetails = (data, id, details) =>
data.map(d => {
return {
...d,
states: d.states.map(s => {
if(s.id !== id){
return s
}
return {
...s,
details
}
})
}
})
const input =
[
{
"country": { "id": 87745195, "action": "Analyze" },
"states": [
{ "id": 83589582, "action": "Verify" },
{ "id": 87335656, "action": "Analyze" }
]
},
{
"country": { "id": 83861166, "action": "Verify" },
"states": [
{ "id": 82175746, "action": "Closed" },
{ "id": 78745158, "action": "Closed" }
]
}
]
const details = { "totalOpenRadars": 1, "totalClosedRadars": 1 }
const id = 82175746
console.log(appendDetails(input, id, details))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473799.html
標籤:javascript 数组 反应 循环 目的
上一篇:倍數之和
