0

I have a scenario where I need to filter the array based on the elements present in another object.

Eg:

x = [{ id:1, name: test},{id:2, name:test1},{id:3, name:test3}]
conditionObj = {test:true, test1:false, test3: false};

my Result should be x = [{id:1, name:test}]

2 Answers 2

2

One liner:

x.filter(elem => conditionObj[elem.name]])

Use filter to filter the array and do a lookup in conditionObj.

Sign up to request clarification or add additional context in comments.

Comments

1
const newObj = x.filter(val => {
    return conditionObj[val.name];
})

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.