3

Based on an array of keys, is there any way to map one object with another object?

var obj1 = { 'firstName': 'Tom', 'lastName': 'Hardy', 'age': 32 }; var obj2 = { 'firstName': 'Jon', 'lastName': 'Snow', 'age': 33 }; keys = ['lastName', 'age']; 

I want to update the value of lastName and age of obj1's by the obj2's value.

Is there any lodash function available for this type of mapping?

2 Answers 2

4

With pure Javascript you can do it using Array#forEach function to iterate over the keys and use [] notation to access the properties based on other variable's value.

var obj1 = { 'firstName': 'Tom', 'lastName': 'Hardy', 'age': 32 }; var obj2 = { 'firstName': 'Jon', 'lastName': 'Snow', 'age': 33 }; var keys = ['lastName', 'age']; keys.forEach(prop => obj1[prop] = obj2[prop]); console.log(obj1);

With lodash#forEach

var obj1 = { 'firstName': 'Tom', 'lastName': 'Hardy', 'age': 32 }; var obj2 = { 'firstName': 'Jon', 'lastName': 'Snow', 'age': 33 }; var keys = ['lastName', 'age']; _.forEach(keys, prop => obj1[prop] = obj2[prop]); console.log(obj1);
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js'></script>

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

5 Comments

is there any lodash/underscore.js mapper available for this type of mapping
lodash is just a library, and it also have a function lodash.com/docs/#forEach with that you can do the same
@URoy - Why would you bother with Lodash or Underscore when it is only 46 characters of plain JS? (And it could even be < 40 using p instead of prop.)
@nnnnnn I don't like one characters variables :)
Meaningful variable names are good, but in a tiny arrow function like this it would be pretty obvious what p meant - but that suggestion was just for the OP if they were looking for maximum brevity. Regarding your Lodash suggestion, I think the OP was looking for a hypothetical _.mapProperties() type function that automatically does this for properties listed in an array, not just an alternative implementation of forEach.
3

You can use lodash#assign and lodash#pick to achieve this result.

_.assign(obj1, _.pick(obj2, keys)); 

var obj1 = { 'firstName': 'Tom', 'lastName': 'Hardy', 'age': 32 }; var obj2 = { 'firstName': 'Jon', 'lastName': 'Snow', 'age': 33 }; var keys = ['lastName', 'age']; _.assign(obj1, _.pick(obj2, keys)); console.log(obj1);
.as-console-wrapper{min-height:100%;top:0;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Alternatively, here's an immutable solution using lodash#pick and lodash#defaults:

var result = _(obj2).pick(keys).defaults(obj1); 

var obj1 = { 'firstName': 'Tom', 'lastName': 'Hardy', 'age': 32 }; var obj2 = { 'firstName': 'Jon', 'lastName': 'Snow', 'age': 33 }; var keys = ['lastName', 'age']; var result = _(obj2).pick(keys).defaults(obj1); console.log(result);
.as-console-wrapper{min-height:100%;top:0;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.