![](/img/trans.png)
[英]How to use _.where to find by ID an object nested in an array of objects?
[英]Iterate nested array of objects, find id and update object matching the id
我有以下输入。 它是一个对象数组,每个对象都有状态,这也是一个对象数组。 当状态 id 与下面提到的id
匹配时,我想在状态对象中附加details
。 即82175746
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
}
});
});
Array.forEach
总是返回undefined
,所以result
总是undefined
。 如果您在操作后查看input
,它确实包含您指定的详细信息。
或者,如果您打算保持input
相同,您可以将input
传播到一个名为result
的新数组中,并对result
执行循环。
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);
您可以使用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)
这应该做
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.