1

I am trying to select multiple objects from a JSON file based on an array of values. To explain it further, I have an array var list = ['test1', 'test2']; and I have a variable that has the following contents:

var data = [
     {
        "unitid": 177834,
        "name": "Test65",
        "year": 2019,
        "HD2019_city": "Kirksville",
     },
     {
        "unitid": 491464,
        "name": "Test77",
        "year": 2019,
        "HD2019_city": "Cerritos",
     },
     {
        "unitid": 459523,
        "name": "Test95",
        "year": 2019,
        "HD2019_city": "Richardson",
     },
     {
        "unitid": 485500,
        "name": "Test1",
        "year": 2019,
        "HD2019_city": "Inglewood",
     },
     {
        "unitid": 134811,
        "name": "Test2",
        "year": 2019,
        "HD2019_city": "Miami",
     }
    ];

I want to retrieve:

    {
        "unitid": 485500,
        "name": "Test1",
        "year": 2019,
        "HD2019_city": "Inglewood",
     },
     {
        "unitid": 134811,
        "name": "Test2",
        "year": 2019,
        "HD2019_city": "Miami",
     }

Then using that result I want to display it to the user with the rest of the key-value pairs.

  • 2
    Does this answer your question? [Javascript filter array by data from another](https://stackoverflow.com/questions/32376651/javascript-filter-array-by-data-from-another) – Keith Jan 11 '21 at 23:40
  • @Keith the provided link does answer my question. I will check it out and revert back to you if I am not able to solve it using that forum answer. – Yashvardhan Khaitan Jan 11 '21 at 23:43
  • `list.map(term => data.find(item => item.name.toLowerCase() == term))` (also, JSON is a text format used to transfer JS objects and other data, it's not relevant to this question, which is about arrays of objects) –  Jan 11 '21 at 23:50

1 Answers1

1
data.filter(item => list.includes(item.name.toLowerCase()))