Search code examples
javascriptarraysjavascript-objects

How to merge two arrays of objects based on the same date


Actually, i want to merge all the products with the same date into the same object as below, i tried lot of algorithms but it doesn't work so that's why i published this post. You can find an example of the data and the expected result.

let obj = {}; for(let i = 0; i < this.data.length; i++){ let date = this.data[i].Date; let p_date = obj[date] || {}; obj[date] = Object.assign(p_date, this.data[i]); } let result = Object.values(obj); return result; 


  this.data = [
                      {
                        "Date": "12/06/2019",
                        "Rank": 1,
                        "ASIN": "B01N9J7MLI",
                        "Rating": "4,1",
                        "Reviews": 4592,
                        "Price": "114,99",
                        "Prime": "No"
                      },
                      {
                        "Date": "12/06/2019",
                        "Rank": 2,
                        "ASIN": "B074QXGBG6",
                        "Rating": 4,
                        "Reviews": 29163,
                        "Price": "60,99",
                        "Prime": "No"
                      }];

expected result :

 this.data = {
                "Date": "12/06/2019",
                "Products": [
                {
                "Date": "12/06/2019",
                "Rank": 1,
                "ASIN": "B01N9J7MLI",
                "Rating": "4,1",
                "Reviews": 4592,
                "Price": "114,99",
                "Prime": "No"
              },
              {
                "Date": "12/06/2019",
                "Rank": 2,
                "ASIN": "B074QXGBG6",
                "Rating": 4,
                "Reviews": 29163,
                "Price": "60,99",
                "Prime": "No"
              }
                ],
              }

Thanks for your answers


Solution

  • Use Array#reduce to accumulate your data to an object with properties for the dates. Foreach object look if in the accumulated object exists a property with this date. If not create it with properties date and Products (with an empty array). In both cases add to this array your current object.
    At the end use Object.values to get the valuesfrom your object to get the desired array.

      this.data = [
        {
          "Date": "12/06/2019",
          "Rank": 1,
          "ASIN": "B01N9J7MLI",
          "Rating": "4,1",
          "Reviews": 4592,
          "Price": "114,99",
          "Prime": "No"
        },
        {
          "Date": "12/06/2019",
          "Rank": 2,
          "ASIN": "B074QXGBG6",
          "Rating": 4,
          "Reviews": 29163,
          "Price": "60,99",
          "Prime": "No"
        }];
        
        let res = Object.values(this.data.reduce((acc, cur) => {
            if (!acc[cur.Date])
                acc[cur.Date] = {Date: cur.Date, Products: []};
            acc[cur.Date].Products.push(cur);
            return acc;
        }, {}));
        
        console.log(res);