简体   繁体   中英

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

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.

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. Create a group object which has each date as key and the object needed in the final output it's value. Use Object.values() to get the values of the group object as an array

 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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