简体   繁体   English

如何基于相同的值合并对象数组?

[英]How to merge Array of Objects based on the same value?

I have this array of Objects: 我有这个对象数组:

var array = [{
    country: "Austria",
    name: "2019-01-04T23:00:00.000Z",
    value: "1"
  },
  {
    country: "Austria",
    name: "2019-01-11T23:00:00.000Z",
    value: "3"
  },
  {
    country: "Austria",
    name: "2019-01-18T23:00:00.000Z",
    value: "1"
  }
]

I want manipulate this to achieve this result: 我想操纵它来实现这个结果:

var array = [{
  country: "Austria",
  series: [{
      name: "2019-01-04T23:00:00.000Z",
      value: "1"
    },
    {
      name: "2019-01-11T23:00:00.000Z",
      value: "3"
    },
    {
      name: "2019-01-18T23:00:00.000Z",
      value: "1"
    }
  ]
}]

I read many questions but none helped me. 我读了很多问题,但没有人帮助我。

This should do: 这应该做:

var map = {};

for(var entity of array) {
    if(!map[entity.country]) {
        map[entity.country] = {
            country: entity.country,
            series: [
                {
                    name: entity.name,
                    value: entity.value
                }
            ]
        };
    }
    else {
        map[entity.country].series.push({
            name: entity.name,
            value: entity.value            
        });
    }
}

var mappedArray = Object.values(map);

Here is functional solution without for loops and mutable variables: 这是没有for循环和可变变量的功能解决方案:

const result = array.reduce((carry, item) => {
    if (!carry.includes(item.country)) {
        carry.push(item.country);
    }
    return carry;
}, []).map(country => {
    return {
        country: country,
        series: array.filter(item => item.country === country).map(item => {
            return {
                name: item.name,
                value: item.value
            };
        })
    };

You could loop thorugh the array. 你可以循环阵列。 Use destructuring to get country and rest of the properties separately. 使用解构来分别获取countryrest属性。 Add each unique country to group object as key and push the rest object to the series array. 将每个唯一的country添加到group对象作为键,并将rest对象推送到series数组。 Then use Object.values() to get the values as an array 然后使用Object.values()将值作为数组获取

 const array=[{country:"Austria",name:"2019-01-04T23:00:00.000Z",value:"1"},{country:"Austria",name:"2019-01-11T23:00:00.000Z",value:"3"},{country:"Austria",name:"2019-01-18T23:00:00.000Z",value:"1"}]; const group = {}; array.forEach(({ country, ...rest }) => { group[country] = group[country] || { country, series: [] }; group[country].series.push(rest) }) console.log(Object.values(group)) 

You can do something like: 你可以这样做:

const result = array
    .map(
        c => ({
            country: c.country,
            series: array
                .filter(d => d.country === c.country)
                .map(
                    d => ({
                        name: d.name,
                        value: d.value
                    })
                )
        })
    )

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

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