Filtering object from an array by checking a matched value in a map (JavaScript)

Dawn17

Let's say I have a data and a map.

let data = [
    { 
        'name': 'bob',
        'items': ['111']
    },
    { 
        'name': 'Jane',
        'items': ['111']
    },
    { 
        'name': 'Greg',
        'items': ['222']
    }
]

let item_map = [
    { 'Item1': '111', 'Item2': '222'}
]

The items in data object contains the ids of the item. What I am trying to do is to filter out the object from data where using a list of values matched to the id of the map.

For example, given ['Item1'], I want to get

[{ 
        'name': 'bob',
        'items': ['111']
    },
    { 
        'name': 'Jane',
        'items': ['111']
}]

What I tried is

data.filter( item => ['111'].some(filter => (item_map["Item1"]).includes(filter)))

But this keeps giving me a Uncaught TypeError: Cannot read property 'includes' of undefined error.

EDIT

data.filter(item => ['111'].some(filter => item_map[0]["Item1"].includes(filter)))

This is what I newly tried, but this just returns all three items.

Amit Beckenstein

I can't exactly understand why you'd choose an array for an items "map", but to conform with your current data structure, this is the correct logic:

let filters = ['Item1'];
data.filter((datum) => {
    return filters.some((filter) => datum.items.includes(item_map[0][filter]));
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Filtering a nested array with objects by checking another nested array with object - JavaScript

From Dev

From array of Object , get array of only matched key value

From Dev

Filtering by value in array in object

From Dev

JavaScript - New object array from matched ID's

From Dev

Filtering Object into two groups by matched object key value

From Dev

Conditional filtering array with map javascript

From Dev

JavaScript object array returned empty when filtering for value of a specific property

From Dev

Is there a way to map a value in an object to the index of an array in javascript?

From Dev

JavaScript filtering Object on a property that is an array

From Dev

Substituting matched characters with a value from object

From Dev

How to return values from a object matched by a array?

From Dev

How to return matched document from Array of object

From Dev

Map New array of object from array of object javascript

From Dev

Selecting matched value from array in JS

From Dev

What is a faster way to get an object from array using find index, or getting value at a key of a map in JavaScript?

From Dev

How to update a object value when matched from an array in one mongodb document?

From Dev

javascript lookup value of a map object which is array of array

From Dev

javascript filtering an array inside an object inside an array

From Dev

How to return inner matched object from nested Array.prototype.find() in JavaScript?

From Dev

Map object with array in javascript

From Dev

jQuery's .map() returns array, not a matched set (jQuery object)

From Dev

How to find a value from array object in JavaScript?

From Dev

sum value from multiple array of object in javascript

From Dev

Javascript get value from an object inside an array

From Dev

Move key/value in object array to other array by matched item

From Dev

Filtering a Map with values from an array using Typescript

From Dev

Return Object value after Filtering an Array

From Dev

Filtering data with key value in the object inside an array

From Dev

Filtering array value of object within objects

Related Related

  1. 1

    Filtering a nested array with objects by checking another nested array with object - JavaScript

  2. 2

    From array of Object , get array of only matched key value

  3. 3

    Filtering by value in array in object

  4. 4

    JavaScript - New object array from matched ID's

  5. 5

    Filtering Object into two groups by matched object key value

  6. 6

    Conditional filtering array with map javascript

  7. 7

    JavaScript object array returned empty when filtering for value of a specific property

  8. 8

    Is there a way to map a value in an object to the index of an array in javascript?

  9. 9

    JavaScript filtering Object on a property that is an array

  10. 10

    Substituting matched characters with a value from object

  11. 11

    How to return values from a object matched by a array?

  12. 12

    How to return matched document from Array of object

  13. 13

    Map New array of object from array of object javascript

  14. 14

    Selecting matched value from array in JS

  15. 15

    What is a faster way to get an object from array using find index, or getting value at a key of a map in JavaScript?

  16. 16

    How to update a object value when matched from an array in one mongodb document?

  17. 17

    javascript lookup value of a map object which is array of array

  18. 18

    javascript filtering an array inside an object inside an array

  19. 19

    How to return inner matched object from nested Array.prototype.find() in JavaScript?

  20. 20

    Map object with array in javascript

  21. 21

    jQuery's .map() returns array, not a matched set (jQuery object)

  22. 22

    How to find a value from array object in JavaScript?

  23. 23

    sum value from multiple array of object in javascript

  24. 24

    Javascript get value from an object inside an array

  25. 25

    Move key/value in object array to other array by matched item

  26. 26

    Filtering a Map with values from an array using Typescript

  27. 27

    Return Object value after Filtering an Array

  28. 28

    Filtering data with key value in the object inside an array

  29. 29

    Filtering array value of object within objects

HotTag

Archive