1

I've got 2 arrays; 1 which contains values by year by order id and 1 of different data and want to merge them into 1 array based on the year.

let array1 = [{orderId: 1, year: 2020, value: 15}, 
              {orderId: 1, year: 2021, value: 20}, 
              {orderId: 1, year: 2022, value: 25},
              {orderId: 2, year: 2020, value: 30}, 
              {orderId: 2, year: 2021, value: 35}, 
              {orderId: 2, year: 2022, value: 40}]
let array2 = [{id: 1, year: 2020, value: 10}, 
              {id: 2, year: 2020, value: 20}, 
              {id: 3, year: 2020, value: 30},
              {id: 1, year: 2021, value: 10}, 
              {id: 2, year: 2021, value: 20}, 
              {id: 3, year: 2021, value: 30}, 
              {id: 1, year: 2022, value: 10}, 
              {id: 2, year: 2022, value: 20}, 
              {id: 3, year: 2022, value: 30}]

What I would like to end up with is something like this:

finalArray = [{year: 2020, array1Values: [15, 30], array2Values: [10, 20, 30]},
              {year: 2021, array1Values: [20, 35], array2Values: [10, 20, 30]},
              {year: 2022, array1Values: [25, 40], array2Values: [10, 20, 30}]]

So it iterates through the years, takes the corresponding value from array1 (2 values; 1 for each orderId), and then array2 (3 values; 1 for each Id).

I'm not really sure where to start with this. My current plan is to create an object like this:

let myObject = {
    year: 0,
    array1Values = [],
    array2Values = []
}

I then intend to loop through array1, create an instance of myObject and set the year. I can do a check on the year before setting it to see if the next element in array1 exists and if the year is greater than the current elements year. If so I'm safe to set the year.

As part of that loop I then intend to filter array1 based on year and push those values into array1Values. At the same time I can sort array2 based on id and then filter on year and push those values into array2Values. Once that's done I can push myObject into finalArray. Is there a better way of doing this?

DeC
  • 2,226
  • 22
  • 42
sr28
  • 4,728
  • 5
  • 36
  • 67
  • you mean like this, `[{year: 2020, values : [15, 30, 10, 20, 30]}, {year: 2021, values: [20, 35, 10, 20, 30]}, {year: 2022, values:[25, 40, 10, 20, 30]}]` – BhaskerYadav Oct 24 '19 at 08:19
  • 2
    Please, fix your final array, it's not a valid JavaScript format :) – Tolotra Raharison Oct 24 '19 at 08:19
  • If you need to merge 2 objects based on year, you should make year an index/property. for example an object like this `let a = {'2010': {values: [10, 20, 30]}}` – canbax Oct 24 '19 at 08:22

2 Answers2

0

Try like this:

Working Demo

  constructor() {
    var groupedData1 = this.groupByKey(this.array1, "year");

    Object.keys(groupedData1).forEach(item => {
      let value = groupedData1[item].map(x => x.value).join(", ");
      this.finalArray.push({
        year: item + ", " + value
      });
    });

    var groupedData2 = this.groupByKey(this.array2, "year");

    Object.keys(groupedData2).forEach(item => {
      let value = groupedData2[item].map(x => x.value).join(", ");
      let keyData = this.finalArray.find(x => x.year.includes(item));
      keyData.year += ", " + value;
      console.log(value);
    });
  }


  groupByKey(data, key) {
    return data.reduce(function(rv, x) {
      (rv[x[key]] = rv[x[key]] || []).push(x);
      return rv;
    }, {});
  }
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

let array1 = [{orderId: 1, year: 2020, value: 15}, 
              {orderId: 1, year: 2021, value: 20}, 
              {orderId: 1, year: 2022, value: 25},
              {orderId: 2, year: 2020, value: 30}, 
              {orderId: 2, year: 2021, value: 35}, 
              {orderId: 2, year: 2022, value: 40}]
let array2 = [{id: 1, year: 2020, value: 10}, 
              {id: 2, year: 2020, value: 20}, 
              {id: 3, year: 2020, value: 30},
              {id: 1, year: 2021, value: 10}, 
              {id: 2, year: 2021, value: 20}, 
              {id: 3, year: 2021, value: 30}, 
              {id: 1, year: 2022, value: 10}, 
              {id: 2, year: 2022, value: 20}, 
              {id: 3, year: 2022, value: 30}]
              
const result = [...array1, ...array2].reduce((acc, {year, value}) => {
    const idx = acc.findIndex(e => e.year === year)
    if(idx === -1) {
      return [
        ...acc,
        {year, values: [value]}
       ]
    } else {
      acc[idx].values.push(value);
      return acc;
    }
  }, []);
  
console.log(result);
Mischa
  • 1,591
  • 9
  • 14