简体   繁体   English

我有一个 object 的数组,需要将其重组为所需的格式。 我尝试使用迭代使用数组解构

[英]I have an array of object which needs to restructured into a desired format. I tried using array destructuring using iteration

The desired output should be as follows.所需的 output 应如下所示。 I tried object restructuring way but i could not push the out as an object. If you can just guide me what are the other array methods i can use to get the desired array我尝试了 object 重组方式,但我无法将其作为 object 推出。如果您能指导我,我可以使用哪些其他数组方法来获取所需的数组

const sample = [
  {
    name: 'Bike',
    series: [
      { date: '01-01-2020', value: '4$' },
      { date: '02-01-2020', value: '3$' },
      { date: '03-01-2020', value: '3.5$' }
    ]
  },
  {
    name: 'Watch',
    series: [
      { date: '01-01-2020', value: '1$' },
      { date: '02-01-2020', value: '2$' },
      { date: '03-01-2020', value: '5$' }
    ]
  }
]

const output = [
  { date: '01-01-2020', 'bike-value': '4$', 'watch-value': '1$' },
  { date: '02-01-2020', 'bike-value': '3$', 'watch-value': '2$' },
  { date: '03-01-2020', 'bike-value': '3.5$', 'watch-value': '5$'}
]

What i tried is as follows.我试过的是如下。 But i cannot make this into a object to push into an empty array.但是我不能把它变成一个 object 来推入一个空数组。

for (const {name: n, series: [{date: d , value: v}]} of sample) {
  console.log('name: ' + n + ', date: ' + d + ', value: ' + v);
}

You could loop through the sample array and then loop through the each series array.您可以循环遍历sample数组,然后循环遍历每个series数组。 Create a group object which has each date as key and the object needed in the final output it's value.创建一个group object,其中每个date作为键,最终 output 所需的 object 是它的值。 Use Object.values() to get the values of the group object as an array使用Object.values()获取group object 的值作为数组

 const sample=[{name:"Bike",series:[{date:"01-01-2020",value:"4$"},{date:"02-01-2020",value:"3$"},{date:"03-01-2020",value:"3.5$"}]},{name:"Watch",series:[{date:"01-01-2020",value:"1$"},{date:"02-01-2020",value:"2$"},{date:"03-01-2020",value:"5$"}]}]; const group = {} for (const { name, series } of sample) { for (const { date, value } of series) { group[date] = group[date] || { date }; group[date][`${name.toLowerCase()}-value`] = value } } const output = Object.values(group) console.log(output)

The group object looks like this:群组 object 如下所示:

{
  "01-01-2020": {
    "date": "01-01-2020",
    "bike-value": "4$",
    "watch-value": "1$"
  },
  "02-01-2020": {
    "date": "02-01-2020",
    "bike-value": "3$",
    ...
   },
  "03-01-2020": {
     ....
  }

A simple nested constructor should work here:一个简单的嵌套构造函数应该在这里工作:

const sample =  [
                    {name : 'Bike', series : 
                        [{date : '01-01-2020', value : '4$'},
                        {date : '02-01-2020', value : '3$'},
                        {date : '03-01-2020', value : '3.5$'}]
                    },
                    {name : 'Watch', series : 
                        [{date : '01-01-2020', value : '1$'},
                         {date : '02-01-2020', value : '2$'},
                         {date : '03-01-2020', value : '5$'}]
                    }

      ];


let results = [];

for (let i = 0; i< sample[0].series.length; i++){
    //get date and 'Bike' value from first value
    let newEntry = {date : sample[0].series[i].date, bikeValue : sample[0].series[i].value};

    //find the corresponding 'Watch' value with another loop
    let watchValue = 0;

    for (let i2 = 0; i2<sample[1].series.length; i2++){
        if(sample[1].series[i2].date == newEntry.date){
            watchValue = sample[1].series[i2].value;
        }
    }

    newEntry.watchValue = watchValue;

    //push new object into results array
    results.push(newEntry);
}

console.log(results);

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

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