简体   繁体   English

将对象数组转换为另一个

[英]Convert array of objects to another

Have a problem with arrays in JS. 在JS中遇到数组问题。 Can't convert from one array to another and i was trying forEach, map, filter, everything what can do with arrays but i can't do this:( Maybe there is another method for that? I was in trying around 3 hours... 无法从一个数组转换为另一个数组,我正在尝试每个可以映射的东西,映射,过滤器,但是我做不到:(也许有另一种方法?我尝试了大约3个小时。 ..

How to convert from this array: 如何从此数组转换:

[
      {
        "id": 1,
        "question": "Do you want to walk?",
        "answers": [
          {
            "id": 1,
            "answer": "Yes"
          }
        ]
      },
      {
        "id": 1,
        "question": "Do you want to walk?",
        "answers": [
          {
            "id": 2,
            "answer": "No"
          }
        ]
      },
      {
        "id": 1,
        "question": "Do you want to walk?",
        "answers": [
          {
            "id": 3,
            "answer": "Maybe"
          }
        ]
      },
      {
        "id": 1,
        "question": "Do you want to walk?",
        "answers": [
          {
            "id": 4,
            "answer": "Never"
          }
        ]
      }
    ]

To this: 对此:

[
      {
        "id": 1,
        "question": "Do you want to walk?",
        "answers": [
          {
            "id": 1,
            "answer": "Yes"
          },
          {
            "id": 2,
            "answer": "No"
          },
          {
            "id": 3,
            "answer": "Maybe"
          },
          {
            "id": 4,
            "answer": "Never"
          }
        ]
      }
    ]

So you want to merge all the objects that have the same ID such that the answers array is a list of the answers from all the objects? 因此,您要合并具有相同ID的所有对象,以使答案数组是所有对象的答案的列表? This sounds like a job for Array.reduce :) Reduce will iterate through the full array and output a new value based on how a callback function manipulates that output value at each iteration. 这听起来像Array.reduce的工作:) Reduce将在整个数组中进行迭代并根据回调函数在每次迭代时如何操纵该输出值来输出新值。

arr.reduce((output, currentValue) => {
    const existing = output.find(element => {
        return element.id === currentValue.id;
    });
    if (existing) {
        existing.answers.push(...currentValue.answers);
    }
    else {
        output.push(currentValue);
    }
    return output;
}, []);

Note I'm using the spread operator ...currentValue.answers here to spread the answers array out into multiple parameters to be pushed. 注意我在这里使用了散布运算符...currentValue.answers将答案数组散布到多个要推送的参数中。

Simply use Array.reduce() to create a map based on id and use Object.values() on the map to get the desired result: 只需使用Array.reduce()创建基于id的地图,然后在地图上使用Object.values()即可获得所需的结果:

 let arr = [ { "id": 1, "question": "Do you want to walk?", "answers": [ { "id": 1, "answer": "Yes" } ] }, { "id": 1, "question": "Do you want to walk?", "answers": [ { "id": 2, "answer": "No" } ] }, { "id": 1, "question": "Do you want to walk?", "answers": [ { "id": 3, "answer": "Maybe" } ] }, { "id": 1, "question": "Do you want to walk?", "answers": [ { "id": 4, "answer": "Never" } ] } ]; let result = Object.values(arr.reduce((acc,obj)=>{ acc[obj.id] = acc[obj.id] || Object.assign({},obj); acc[obj.id].answers = (acc[obj.id].answers || []).concat(obj.answers); return acc; },{})); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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