0

Suppose I have an object a as a = {id: 1, name: 'abc'}.

And I have another object b as

b = {id:2} 

OR

b = {name: 'trp'} 

OR

b = {} 

Now, I want to be able to compare and assign a and b (like a=b) in such a way, that whichever property b holds it updates in a, and if it doesn't hold any property, a remains the same.

eg

a= {id: 1, name: 'abc'} b= {name: 'trp'} a=b //should give output a= {id: 1, name: 'trp'} 

ALSO it might be that the solution to this question is pretty simple. VERY NICE. just post it. Whatever the solution is, it's clearly tripping over my head.

Arigatou gozaimasu

EDIT there can also be a third scenario where

b = {id: 2, name: 'trp', title:'not required'} 

in that case also the statement comparing and assigning values of b to a should give result {id: 2, name: 'trp'} i.e. No merging

5
  • 3
    your a=b can be achieved using a = Object.assign(a, b); ... but as for comparing? that's complex Commented Feb 22, 2018 at 13:42
  • 2
    Asking a duplicate question and having it marked as such is not a punishment. It's a way of making your question more useful. It'll still be there so that people who happen upon the problem can use your wording as a way of getting to a good answer quickly. Commented Feb 22, 2018 at 13:44
  • you could use lodash for this there is a merge method there. lodash.com/docs/4.17.5#merge Commented Feb 22, 2018 at 13:50
  • @Pointy after whole day working with code I was just trying to funny here(PewDiePie style) I know wrong place and timing. :) Commented Feb 23, 2018 at 4:04
  • @JaromandaX thank you this is exactly what I want. Simple, short and up to the point. You can post it as answer if you want I'll accept it. Also for the comparison part, I meant that comparison will be made to check the difference in values of same keys and update acc to it. Like a has id =1, b has id =2, there's a difference in key 'id' and so a id will be updated. Commented Feb 23, 2018 at 4:31

4 Answers 4

2

The simplest way to "merge" these two objects is using Object.assign (don't worry, it can be polyfilled for Inernet Explorer)

a = {'id': 1, 'name': 'abc'} b = {'name': 'trp'} a = Object.assign(a, b) 

results in

a == {'id': 1, 'name': 'trp'} 

any property with same key in b will overwrite a (because the assign is performed in order, left to right or a then b in the code example, any existing keys are overwritten with new values

Sign up to request clarification or add additional context in comments.

Comments

1

const a = { id: 1, name: 'abc'}; const b = {name: 'trp', title: 'edfg'}; const results = Object.keys(a).reduce((acc, key) => { acc[key] = b[key] || a[key]; return acc; }, {}) console.log(results);

3 Comments

if a = { id: 1, name: 'testA'}; b = { name: 'testB', title: 'out of topic'}; the accepted answer will be a = { id: 1, name: 'testB', title: 'out of topic'}; But the title shouldn't be in there. Can you please update your question as per answer? Solution: If you use ES6, it will be a single line { ...a, ...b }; If its right, I will update my answer as well
hey excellent observation! That scenario didn't occur to me. Again my fault, bcoz the answer given earlier clearly mentions "merge" and and I didn't pay attention. I'll edit my question. Thanks
@nutnoltu Actually I think I'm going to accept your answer instead of editing my question to the previous answer bcoz you brought in an imp scenario which skipped my mind while writing the question. Also other than not covering all the cases, Object.assign() and the spread property {...} have their own performance concern or still being in proposal stage. So yes, your answer is correct.
0

you can just run the 'b' object in a for loop and replace the similar items in 'a' object having matching key names.

$.each(b, function (key, val) { a[key][]=val; }); 

Thanks, hope this solves you problem.!!

1 Comment

nice.. thats a clean solution and new knowledge :)
0

here's what you can try, it may not be the best solution but it worked for me :

var a= {id: 1, name: 'abc', test:'test'} var b= {name: 'trp'} for (var key in a) { if(key in b){ a[key]=b[key]; } } for (var key in b) { if(!key in a){ a[key]=b[key]; } } 

to try the output :

for (var key in a) { if(key in b){ alert(a[key]); } } 

This way if there is a key/value in b not present in a, a will also get it

you can turn this into a function :

function mergeArrays(a,b){ for (var key in a) { if(key in b){ a[key]=b[key]; } } for (var key in b) { if(!key in a){ a[key]=b[key]; } } return a; } 

1 Comment

Thank you but please check the first comment under my post. That is the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.