Samman Thapa Samman Thapa - 1 year ago 89
Node.js Question

How to create a list of items from another list based on if the items are the key of an object? (Lodash preferred)

How to say this in a more eloquent way, preferably using Lodash:

anObject = {'a': 'apple', 'b': 'banana'};
largeArray = ['a','c','d'];
emptyArray = [];

for(var s = 0; s < largeArray.length; s++){

Should give me:

emptyArray = ['a']

Answer Source

You can get the intersection() from the largeArray and the keys() from the anObject variable to accomplish this.

_.intersection(largeArray, _.keys(anObject));

var anObject = {'a': 'apple', 'b': 'banana'};
var largeArray = ['a','c','d'];

var emptyArray = _.intersection(largeArray, _.keys(anObject));

<script src=""></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download