1

I have two objects which I am trying to merge with different key The first object I need to compare with Id and my second object I need to compare with Option.

And My final expected output i have mentioned below

Apologize my bad formatting . I'm literally new to this forum. Please help.

Thanks in Advance.

My First Object

[
  {
    Category__c: "Option",
    Description__c: "Exdoor",
    Id: "a0F1e000000EgcGEAS",
    Name: "with door"
  },
  {
    Category__c: "Upgrade",
    Description__c: "Exdoor",
    Id: "a0F1e000000EgcHEAS",
    Name: "Upgrade"
  },
  {
    Category__c: "New Option",
    Description__c: "1300 wide with doors",
    Id: "a0F1e000000EgcIEAS",
    Name: "doors"
  },
  {
    Category__c: "Oven",
    Description:"1450 Door",
    Id: "a0F1e000000EgcJEAS",
    Name: "Oven Option: Wall Oven"
  }
]

My second Object

[
  {
    Option: "a0F1e000000EgcJEAS",
    Price: 0
  }
]

Now My expected output is

[
  {
    Category__c: "Option",
    Description__c: "Exdoor",
    Id: "a0F1e000000EgcGEAS",
    Name: "with door"
  },
  {
    Category__c: "Upgrade",
    Description__c: "Exdoor",
    Id: "a0F1e000000EgcHEAS",
    Name: "Upgrade"
  },
  {
    Category__c: "New Option",
    Description__c: "1300 wide with doors",
    Id: "a0F1e000000EgcIEAS",
    Name: "doors"
  },
  {
    Category__c: "Oven",
    Description:"1450 Door",
    Id: "a0F1e000000EgcJEAS",
    Price: 0,
    Name: "Oven Option: Wall Oven"
  }
]
nouvist
  • 1,107
  • 10
  • 24
grey 123
  • 11
  • 2

3 Answers3

1

You can use Array.prototype.forEach() which executes a provided function once for each array element.

In the forEach we will check if the current data.id equals to the selected option. if so, we will use the dot notation to add it's price to the data.

Edited : i updated the code to grey 123 request and changed selected to be array and for his request

const data = [
{
Category__c: "Option",
Description__c: "Exdoor",
Id: "a0F1e000000EgcGEAS",
Name: "with door"

},
{
 Category__c: "Upgrade",
Description__c: "Exdoor",
Id: "a0F1e000000EgcHEAS",
Name: "Upgrade"

},
{
Category__c: "New Option",
Description__c: "1300 wide with doors",
Id: "a0F1e000000EgcIEAS",
Name: "doors"
},
{
Category__c: "Oven",
Description:"1450 Door",
Id: "a0F1e000000EgcJEAS",
Name: "Oven Option: Wall Oven"
}];

const options = [{
     Option: "a0F1e000000EgcJEAS",
     Price: 0
}];

data.forEach(x => {
  const selected = options.find(y => y.Option === x.Id);
  if (selected){
  x.Price = selected.Price;
  }
})

console.log(data);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
1

It would be better not to mutate the original object instead make changes in new object.

const data = [
  {
    Category__c: "Option",
    Description__c: "Exdoor",
    Id: "a0F1e000000EgcGEAS",
    Name: "with door",
  },
  {
    Category__c: "Upgrade",
    Description__c: "Exdoor",
    Id: "a0F1e000000EgcHEAS",
    Name: "Upgrade",
  },
  {
    Category__c: "New Option",
    Description__c: "1300 wide with doors",
    Id: "a0F1e000000EgcIEAS",
    Name: "doors",
  },
  {
    Category__c: "Oven",
    Description: "1450 Door",
    Id: "a0F1e000000EgcJEAS",
    Name: "Oven Option: Wall Oven",
  },
];

const selected = [
  {
    Option: "a0F1e000000EgcJEAS",
    Price: 0,
  },
];

const result = data.map((obj) => {
  const searchObj = selected.find(({ Option, Price }) => Option === obj.Id);
  if (searchObj) return { ...obj, Price: searchObj.Price };
  return obj;
});

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
1
Below will do

let one = [{
        Category__c: "Option",
        Description__c: "Exdoor",
        Id: "a0F1e000000EgcGEAS",
        Name: "with door",

    },

    {
        Category__c: "Upgrade",
        Description__c: "Exdoor",
        Id: "a0F1e000000EgcHEAS",
        Name: "Upgrade",

    },

    {
        Category__c: "New Option",
        Description__c: "1300 wide with doors",
        Id: "a0F1e000000EgcIEAS",
        Name: "doors",

    },
    {
        Category__c: "Oven",
        Description: "1450 Door",
        Id: "a0F1e000000EgcJEAS",
        Name: "Oven Option: Wall Oven"
    }
]

let two = [{
    Option: "a0F1e000000EgcJEAS",
    Price: 0,
}]

function merge(one, two, oneKey, twoKey) {
  var byMap = {}
  two.forEach((el)=> {
    byMap[el[twoKey]] = el;
  });
  one.forEach((el)=> {
    Object.assign(el, byMap[el[oneKey]] || {});
  });
}
merge(one, two, "Id", "Option")
console.log(one)
Karthikeyan
  • 406
  • 3
  • 14