簡體   English   中英

迭代嵌套的對象數組,查找 id 並更新與 id 匹配的對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM