1

Here is my array of two values .

let dataList = ["x","y","z","a","b"]
let data2= {
  x:{hide:true},
  y:{hide:true},
  z:{},
  a:{}
}

here is my trying code:

let filters = dataList.filter(item=>Object.keys(data2).includes(item))

I want to filter dataList based data2 - hide:true . For example, if values object property hide:true inside data2 , key will be removed .

expected output :

["z","a"]

2 Answers 2

2

I believe it is a simple as this

let dataList = ["x","y","z","a","b"]
let data2= {
  x:{hide:true},
  y:{hide:true},
  z:{},
  a:{}
}

let filters = dataList.filter(item=> data2[item] && !data2[item].hide)

console.log(filters)

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

Comments

1

You can check if the key exists on data2 and check for hide to be true

dataList.filter(item => data2[item] && !data2[item].hide )

2 Comments

You beat me by 30 seconds, because of my snippet ;)
yeah, no snippets for me :)

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.