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

qjp7pelc  于 2023-08-02  发布在  Java
关注(0)|答案(4)|浏览(174)

我有下面的输入如下。它是一个对象数组,每个对象都有状态,这也是一个对象数组。我想在states对象中添加details,当状态id与下面提到的id匹配时。即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
        }

    });
});

41ik7eoe

41ik7eoe1#

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);

字符串

vfwfrxfs

vfwfrxfs2#

您可以使用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)

字符串

uoifb46i

uoifb46i3#

这应该是做它

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))

字符串

xlpyo6sf

xlpyo6sf4#

可重用的方法不仅仅是键或值,每个可能的元素都以整洁干净的方式呈现。
你可以改变数组,对象的键在哪里查找,键在哪里要与值匹配,以及新的键和新的值。

const students = [
    {
      "name": "Amoos",
      "skills": [
        { id: 11, name: "html" },
        { id: 12, name: "css" }
      ],
      "sports": [
        {id: 31, name: "football"},
        {id: 32, name: "circket"},
      ]
    },
    {
      "name": "Rifat",
      "skills": [
        { id: 21, name: "reactjs" },
        { id: 22, name: "nextjs" }
      ],
      "sports": [
        {id: 41, name: "baseball"},
        {id: 42, name: "volleyball"},
      ]
    },
]

 

const addNewValueByKeyInArray = (arrayData, where, key, value, newKey, newValue ) => {
   return arrayData.map(( d ) => d[where].map((w) => (w[key] === value) ? {
            ...w, [newKey]: newValue
        } : {...w}
    ))
}

// EXAMPL #1
console.log(addNewValueByKeyInArray(students, 'sports', 'id', 42, 'details', { 'region': 'worldwide', 'first_played': '1895' }));

// EXAMPL #2
console.log(addNewValueByKeyInArray(students, 'skills', 'id', 22, 'details', { 'author': 'Guillermo Rauch', 'first_version': '2016' }));

字符串

相关问题