Taieb
Taieb

Reputation: 920

How can I merge Objects inside the array of objects depending on specific field?

I have an array containing objects :

let Array1 =  

[
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "60f04b190161e73c42a512df",
        "uid": "QB32N-20210715-20758",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "QB32N",
        "version_application": "1.1.0",
        "creation_date": null,
        "tit": "RESTA",
        "ref": "162367462",
        "description": "300x300 - 20 - Blue",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
      }
    ]
  },
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "456d465d45d89a7da8954d564a",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "Lova",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
  }]
  },
    {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA74",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
  }]
  },
      {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA75",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN75",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
  }]
  },
 {
    "nom": "TEST3",
    "parent": "QC",
    "products": [
      {
        "Id": "ELA76",
        "uid": "7a897897-789745456123-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN76",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST3",
          "parent": "QC"
        }
  }
    ]
  },

]

I want to merge products of objects depending on the field nom & parent, as soon as this equal to another object i want to merge it and add the product in the array so instead of having many redundant objects with the same nom & parent and other fields if the nom and parents are the same on many objects merge them and add merge the array products too. so it will look like something like this:

let final = [
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "60f04b190161e73c42a512df",
        "uid": "QB32N-20210715-20758",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "QB32N",
        "version_application": "1.1.0",
        "creation_date": null,
        "tit": "RESTA",
        "ref": "162367462",
        "description": "300x300 - 20 - Red",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
      },
            {
        "Id": "456d465d45d89a7da8954d564a",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "Lova",
        "ref": "462378462",
        "description": "700X700 - 20 - Cross Black",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
  }
    ]
  },
  },
    {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA74",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
  },
        {
        "Id": "ELA75",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN75",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
  }
  },
 {
    "nom": "TEST3",
    "parent": "QC",
    "products": [
      {
        "Id": "ELA76",
        "uid": "7a897897-789745456123-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN76",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST3",
          "parent": "QC"
        }
  }
  },

]

Upvotes: 1

Views: 181

Answers (2)

Nitheesh
Nitheesh

Reputation: 19986

Try this buy using normal loop implemntation.

let Array1 = [
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "60f04b190161e73c42a512df",
        "uid": "QB32N-20210715-20758",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "QB32N",
        "version_application": "1.1.0",
        "creation_date": null,
        "tit": "RESTA",
        "ref": "162367462",
        "description": "300x300 - 20 - Blue",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
      }
    ]
  },
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "456d465d45d89a7da8954d564a",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "Lova",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
      }
    ]

  },
  {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA74",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
      }
    ]
  },
  {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA75",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN75",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
      }
    ]
  },
  {
    "nom": "TEST3",
    "parent": "QC",
    "products": [
      {
        "Id": "ELA76",
        "uid": "7a897897-789745456123-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN76",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST3",
          "parent": "QC"
        }
      }
    ]
  },
]
const output = [];
let arr2 = Array1.forEach((node) => {
  const findNode = output.find((item) => item.nom === node.nom && item.parent === node.parent);
  if (findNode) {
    findNode.products = findNode.products.concat(node.products);
  } else {
    output.push(node);
  }
})
console.log(output);

Edit Array.reduce implementation.

let Array1 = [
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "60f04b190161e73c42a512df",
        "uid": "QB32N-20210715-20758",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "QB32N",
        "version_application": "1.1.0",
        "creation_date": null,
        "tit": "RESTA",
        "ref": "162367462",
        "description": "300x300 - 20 - Blue",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
      }
    ]
  },
  {
    "nom": "TEST",
    "parent": "QB",
    "products": [
      {
        "Id": "456d465d45d89a7da8954d564a",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "Lova",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST",
          "parent": "QB"
        }
      }
    ]

  },
  {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA74",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN74",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
      }
    ]
  },
  {
    "nom": "TEST2",
    "parent": "QX",
    "products": [
      {
        "Id": "ELA75",
        "uid": "7a897897-20210715-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN75",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST2",
          "parent": "QX"
        }
      }
    ]
  },
  {
    "nom": "TEST3",
    "parent": "QC",
    "products": [
      {
        "Id": "ELA76",
        "uid": "7a897897-789745456123-20756",
        "type": "fabricant",
        "statut": "publié",
        "app_ref": "RN76",
        "version_application": "1.1.0",
        "creation_date": "2021-07-15T14:42:41.465Z",
        "tit": "RESTA",
        "ref": "462378462",
        "description": "300x300 - 20 - Cross Black",
        "famille": {
          "nom": "TEST3",
          "parent": "QC"
        }
      }
    ]
  },
]
let arr2 = Array1.reduce((acc, node) => {
  const findNode = acc.find((item) => item.nom === node.nom && item.parent === node.parent);
  if (findNode) {
    findNode.products = findNode.products.concat(node.products);
  } else {
    acc.push(node);
  }
  return acc;
}, [])
console.log(arr2);

Upvotes: 1

AdityaParab
AdityaParab

Reputation: 7100

Just do a reduce operation on your original array and see if the object with same nom property already exists. If so, check if the current item in the array has the same parent. If so, just add your cur.products to your already existing element and then return the accumulated result from the reduce.

const result = Array1.reduce((acc, cur) => {
  const doesExist = acc.find(a => a.nom === cur.nom);
  if(doesExist) {
    const doesParentExist = acc.find(a => a.parent === cur.parent)
    if(doesParentExist) {
      doesExist.products = [...doesExist.products, ...cur.products]
    } 
  } else {
    acc.push(cur);
  }
  return acc;
}, []);

Upvotes: 3

Related Questions