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

Samman Bikram Thapa

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++) {
  if (anObject[largeArray[s]]) {
    emptyArray.push(anObject[largeArray[s]]);
  }
}

console.log(emptyArray)

Should give me:

emptyArray = ['a']
ryeballar

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));

console.log(emptyArray);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create list of items from another list

From Dev

How to filter one list of items from another list of items?

From Dev

Create a list with items from another list at indices specified in a third list

From Dev

Create list from list items

From Dev

removing items from list that exist in another list based on an ID member

From Dev

removing items from list that exist in another list based on an ID member

From Dev

How to move Items from one list to the another list in python?

From Dev

How to create a new list with items from an old list?

From Dev

Excluding items from a list that exist in another list

From Dev

c# -How to put items from a List<string> to List<object>

From Dev

Remove items from a list in Python based on previous items in the same list

From Dev

Python - Filter list of dictionaries based on if key value contains all items in another list

From Dev

How to remove items from a list that contains words found in items in another list

From Dev

How to remove items from a list that contains words found in items in another list

From Dev

Create IGrouping based on sub List items

From Dev

create a list of cardview based on items count android

From Dev

How to unpack items from a List?

From Dev

How to unpack items from a List?

From Dev

How to add items into a ListView from a List of another class

From Dev

How to copy only new items from one list to another

From Dev

Python how to strip a string from a string based on items in a list

From Dev

Python how to strip a string from a string based on items in a list

From Dev

Java 8 streams: find items from one list that match conditions calculated based on values from another list

From Dev

Remove list of items from another file in bash

From Dev

find lists that start with items from another list

From Dev

Find items from List based on matching property

From Dev

Find items from the list based on given date

From Dev

C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)

From Dev

C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)

Related Related

  1. 1

    How to create list of items from another list

  2. 2

    How to filter one list of items from another list of items?

  3. 3

    Create a list with items from another list at indices specified in a third list

  4. 4

    Create list from list items

  5. 5

    removing items from list that exist in another list based on an ID member

  6. 6

    removing items from list that exist in another list based on an ID member

  7. 7

    How to move Items from one list to the another list in python?

  8. 8

    How to create a new list with items from an old list?

  9. 9

    Excluding items from a list that exist in another list

  10. 10

    c# -How to put items from a List<string> to List<object>

  11. 11

    Remove items from a list in Python based on previous items in the same list

  12. 12

    Python - Filter list of dictionaries based on if key value contains all items in another list

  13. 13

    How to remove items from a list that contains words found in items in another list

  14. 14

    How to remove items from a list that contains words found in items in another list

  15. 15

    Create IGrouping based on sub List items

  16. 16

    create a list of cardview based on items count android

  17. 17

    How to unpack items from a List?

  18. 18

    How to unpack items from a List?

  19. 19

    How to add items into a ListView from a List of another class

  20. 20

    How to copy only new items from one list to another

  21. 21

    Python how to strip a string from a string based on items in a list

  22. 22

    Python how to strip a string from a string based on items in a list

  23. 23

    Java 8 streams: find items from one list that match conditions calculated based on values from another list

  24. 24

    Remove list of items from another file in bash

  25. 25

    find lists that start with items from another list

  26. 26

    Find items from List based on matching property

  27. 27

    Find items from the list based on given date

  28. 28

    C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)

  29. 29

    C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)

HotTag

Archive