简体   繁体   English

迭代嵌套的对象数组,查找 id 并更新与 id 匹配的对象

[英]Iterate nested array of objects, find id and update object matching the id

I have below input as follows.我有以下输入。 Its an array of objects and each object has states which is also a array of objects.它是一个对象数组,每个对象都有状态,这也是一个对象数组。 i want to append details inside the states object when the state id matches with the id mentioned below.当状态 id 与下面提到的id匹配时,我想在状态对象中附加details ie 8217574682175746

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

And this is the result i am trying to achieve.这就是我想要达到的结果。 Please note that id of 82175746 is compared with all the state ids.请注意,将 82175746 的 id 与所有状态 id 进行比较。 once a match is found, details mentioned above is appended as shown below to the matched object.一旦找到匹配项,上述详细信息将如下所示附加到匹配的对象中。

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" }
        ]
    }
]

In order to achieve this, I tried this way but I am not able to get the result properly.为了实现这一点,我尝试了这种方式,但我无法正确获得结果。 Can someone please let me know where I went wrong有人可以让我知道我哪里出错了吗

const result  = input.forEach((element) => {
    element.states.forEach((state) => {
        if(state.id === id) {
            state.details = details
        }

    });
});

Array.forEach always returns undefined , so result will always be undefined . Array.forEach总是返回undefined ,所以result总是undefined If you look at input after your operation, it does contain your details as you specified.如果您在操作后查看input ,它确实包含您指定的详细信息。

Alternatively, you could spread input into a new array called result and perform your loop on result instead if you intend to keep input the same.或者,如果您打算保持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);

You can use Array.prototype.map() :您可以使用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)

this should be do it这应该做

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM