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.

3
  • 2
    Does this answer your question? Javascript filter array by data from another Commented Jan 11, 2021 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. Commented Jan 11, 2021 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) Commented Jan 11, 2021 at 23:50

1 Answer 1

1
data.filter(item => list.includes(item.name.toLowerCase()))
Sign up to request clarification or add additional context in comments.

Comments

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.