0

Got two objects:

master  = [ { "id": 411, "state": 1 },
            { "id": 2134, "state": 1 },
            { "id": 2135, "state": 0 },
            { "id": 2137, "state": 0 } ]

 zips = [ { "id": 411, "zip": "90201" },
          { "id": 412, "zip": "90201" },
          { "id": 2134, "zip": "90201" },
          { "id": 2137, "zip": "90201" } ]

I'd like to merge them together only if the id in zips exists in devices. Using the above data, the result should be:

master  = [ { "id": 411, "state": 1, "zip": "90201" },
            { "id": 2134, "state": 1, "zip": "90201" },
            { "id": 2135, "state": 0 },
            { "id": 2137, "state": 0, "zip": "90201" } ]

id: 412 is zips is 'skipped' because it's not in the devices object.

I've looked at looping through functions like Object.assign or merging with ... , but they have undesirable results (like adding the id 412 above because it exists in the second object)

Any hints or tips or better places to look are greatly appreciated!

the digitalmouse
  • 223
  • 3
  • 25
  • Does this post help you → https://stackoverflow.com/questions/40573555/merge-two-objects-but-only-existing-properties? – zrna Jun 30 '21 at 10:16
  • 2
    `master.map(o=>({...o,...zips.find(z=>z.id===o.id)}));` – Jaromanda X Jun 30 '21 at 10:21
  • Does this answer your question? [Merge two objects but only existing properties](https://stackoverflow.com/questions/40573555/merge-two-objects-but-only-existing-properties), or [Merge two array of objects based on a key](https://stackoverflow.com/questions/46849286/merge-two-array-of-objects-based-on-a-key) – pilchard Jun 30 '21 at 10:22
  • @jabaa correct! good catch. question edited accordingly. Just got used to pulling JSON into an object via a microservice and calling it still JSON. :P – the digitalmouse Jun 30 '21 at 10:25
  • @zrna - almost! but that appears to remove and differences and keeps only the same entries between the two objects. – the digitalmouse Jun 30 '21 at 10:26
  • 1
    seriously ... `master.map(o=>({...o,...zips.find(z=>z.id===o.id)}));` - of course, that won't change master :p - if you want to mutate master ... `master.forEach((o,i,a)=>a[i]={...o,...zips.find(z => z.id === o.id)});` – Jaromanda X Jun 30 '21 at 10:27
  • @JaromandaX - doh! I keep forgetting about .map in my old age! Thanks! :) – the digitalmouse Jun 30 '21 at 10:32

2 Answers2

1

You can easily achieve the result using map

const result = master.map((obj) => ({
  ...obj,
  ...zips.find((o) => o.id === obj.id),
}));

master = [
  { id: 411, state: 1 },
  { id: 2134, state: 1 },
  { id: 2135, state: 0 },
  { id: 2137, state: 0 },
];

zips = [
  { id: 411, zip: "90201" },
  { id: 412, zip: "90201" },
  { id: 2134, zip: "90201" },
  { id: 2137, zip: "90201" },
];

const result = master.map((obj) => {
  const { id } = obj;
  const objThatExist = zips.find((o) => o.id === id);
  return { ...obj, ...objThatExist };
});

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

I solve this with nested each. I look every master objects and control that there are any same ids. if they are same values , push the zip into master object

jQuery

$.each(zips,(index,object)=>{
                var zipID=object.id;
                $.each(master,(indexMaster,objectMaster)=>{
                    if(zipID==objectMaster.id){
                        objectMaster['zip']=zipID
                    }
                })
            })
            console.log(master);

JavaScript

zips.forEach(index,object){
                var zipID=object.id;
                master.forEach(indexMaster,objectMaster){
                    if(zipID==objectMaster.id){
                        objectMaster['zip']=zipID
                    }
                }
            }
icsarisakal
  • 193
  • 7