0

I have two objects and I want merge it together by articlenumber and add new fields in a detail key. I tried it with lodash but I got a flat object.

1st Object:

products = {
 articlenumber: '1',
 name: 'Super Awesome Product',
 category: 'magic'
}

2nd Object:

productDetails = {
 articlenumber: '1',
 color: 'red',
 size: 'xxl'
}

My wish:

products = {
  articlenumber: '1',
  name: 'Super Awesome Product',
  category: 'magic',
  details: {
    color: 'red',
    size: 'xxl'
  },
}, 
{
  ...
}

What I did:

 var merged = _.merge(_.keyBy(products, 'articlenumber'), _.keyBy(productDetails, 'articlenumber'));

What I got:

products = {
  articlenumber: '1',
  name: 'Super Awesome Product',
  category: 'magic',
  color: 'red',
  size: 'xxl'
}, 
{
  ...
}
Romano
  • 5
  • 2
  • Your "My wish" `products` is not a valid Javascript Object. Are you looking to end up with an array of objects? – mykaf May 03 '21 at 14:56
  • 2
    is "products" an array of objects? I think it's a typo that it's shown as a single object Because your output shows an array but without the braces – Shubham Periwal May 03 '21 at 14:57

2 Answers2

0

There are many ways to merge:

To merge:

Please read the links below:

How can I merge properties of two JavaScript objects dynamically?

To remove duplicates:

How to remove all duplicates from an array of objects?

let products = {
    articlenumber: '1',
    name: 'Super Awesome Product',
    category: 'magic'
 };
   
let productDetails = {
    articlenumber: '1',
    color: 'red',
    size: 'xxl'
};

let merged = {...products,details: productDetails};

console.log(merged);
Erfan Bahramali
  • 392
  • 3
  • 13
0

If Products is indeed an object, @Erfan's answer is gold. Assuming Products is an array, you can try:

products = [{
 articlenumber: '1',
 name: 'Super Awesome Product',
 category: 'magic'
}]

productDetails = [{
 articlenumber: '1',
 color: 'red',
 size: 'xxl'
}]

products.forEach((prod) => {
    var productDetail = productDetails.filter((prodDetail) => {return prod['articlenumber']===prodDetail['articlenumber']})[0]

    var productToAdd = {}
    var fields = ['color', 'size']
    fields.forEach((field) => productToAdd[field] = productDetail[field])

  prod['details'] = productToAdd
});

console.log(products)
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26
  • Thank you for the fast reply. Is it also possible to enter all fields via a loop in "details"? For this example I wrote only two but I've more. – Romano May 03 '21 at 15:38
  • yeah sure you can use `delete productDetail.articlenumber` and then just directly do `prod['details'] = productDetail` and all the fields except `articlenumber` will be added. – Shubham Periwal May 03 '21 at 15:42
  • I've also edited the answer so you can specify the fields you want. Hope it helps. Do consider accepting the answer if it works for you – Shubham Periwal May 03 '21 at 15:48
  • 1
    You are magic. Thank you so much for your fast and friendly help @ShubhamPeriwal – Romano May 03 '21 at 15:53