0

I am trying to achieve the following task with lodash or pure JS.

I have an Array of Objects which I filter by a given property value


let res = _.filter(array, {obj.property: "somevalue"}

Now I have another Array like so [val1, val2,val3] which I want the filtered array properties to compare with like so.

let res = _.filter(array, {obj.property1: "somevalue", obj.property2: existsInOtherArray}
1
  • Can you provide an example with the input and expected output? Commented Sep 19, 2021 at 20:00

1 Answer 1

1

You can use a set and the filter method of array like:

const array = [
  {property1: 'somevalue'},
  {property1: 'somevalue', property2: 'val2'}
]

const set = new Set(['val1', 'val2', 'val3'])
const res = array.filter(obj => obj.property1 === 'somevalue'
                                && set.has(obj.property2))

console.log(res)

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.