1

I've an object and the array as shown below and I wanted to remove all the properties of the object except those listed in the array. Can someone help me with a good lodash function to do this.

object : {A:1,B:2,C:3,D:4} 
array: [{type:B,value:xyz,order:1},{type:D,value:abc,order:0}]

I want the object to be {B:2,D:4} after the operation
4
  • Can be it done in pure js? Commented Mar 13, 2017 at 21:03
  • @Kinduser, thanks for the response. I know how to do it in pure js, but I'm looking for a solution using lodash as we've been using that in our project and wanted to learn the same. Commented Mar 13, 2017 at 21:07
  • do you want to mutate the object, or just filter it, or does it not matter? Commented Mar 13, 2017 at 21:11
  • @orion, I want to mutate the object, so that the object contains only the fields that are present in array. Commented Mar 13, 2017 at 21:14

3 Answers 3

3

Use _.pick like this:

var result = _.pick(object, array);

var object = {
  A: 1,
  B: 2,
  C: 3,
  D: 4
};

var array = ["B", "D"];

var result = _.pick(object, array);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

EDIT:

You should first use _.map to get the keys and then use _.pick like this:

var result = _.pick(object, _.map(array, function(e) {
  return e.type;
}));

or using arrow functions like this:

var result = _.pick(object, _.map(array, e => e.type));

or even shorter using the iteratee argument like this:

var result = _.pick(object, _.map(array, "type"));

var object = {
  A: 1,
  B: 2,
  C: 3,
  D: 4
};

var array = [{type: "B", value: "xyz", order: 1}, {type: "D", value: "abc", order: 0}];

var result = _.pick(object, _.map(array, "type"));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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

6 Comments

hi, ibrahim thanks for the answer. First of all my apologies, I missed the crucial part of my array at least I believe which I've modified now. I realized about that mistake in my question only after looking at your answer as I've already read about pick before posting my question.
@AjaySrikanth see the EDIT section above!
Thanks ibrahim. This is what I wanted.
you can shorten this with var result = _.pick(object, _.map(array, 'type'));
@ryeballar yeah even clearer, didn't know you can do that so thanks!
|
2

You can use Object.assign(), Array.prototype.map() , spread element, computed property, to assign result of filtering type properties to object

var object = {A:1,B:2,C:3,D:4} 
var array = [{type:"B",value:"xyz",order:1},{type:"D",value:"abc",order:0}];
object = Object.assign({}, ...array.map(({type}) => ({[type]:object[type]})));

console.log(object);

2 Comments

what is the ... in Object.assign() ? Could you please explain that syntax. I've just started as full time js developer. I'm having trouble understanding few syntax here and there.
0

You can use _.pick along with _.map to achieve this:

let obj = { A:1, B:2, C:3, D:4 };
let arr = [{ type:'B', value:'xyz', order:1 },{ type:'D', value:'abc', order:0 }];

let output = _.pick(obj, _.map(arr, 'type'));

console.log(output); // { B:2, D: 4 }

Example: JsFiddle

Lodash Docs:

_.map(): link

_.pick(): link

2 Comments

Check the desired output again!
@ibrahimmahrir Ah pants! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.