Array.filter
takes a function which should return true or false, which indicates whether a particular element from an array should be retained or not.
You have an array objects, and objects themselves can have multiple keys and values. You need a multi-step process here:
- Reduce your array objects into a single object (this might be a problem if multiple objects contain the same keys!)
- Select the keys out of your combined object with a value of true.
You can use Object.assign to merge objects. We'll use Array.reduce to reduce the array of objects into a single object:
const combined = arry.reduce((acc, val) => Object.assign({}, acc, val))
This will result in a single object containing all your key-value pairs:
// { country: false, fname: true, lname: true}
Now we just need to select the entries which have a true value, which results in an array of arrays:
const entries = Object.entries(combined).filter(([k, v]) => v)
// [["lname", true], ["fname", true]]
Then we can map out the keys from that array, like so:
entries.map(([k, v]) => k)
// ["lname", "fname"]
These steps could all be chained together for brevity.
const arry = [{'fname': true}, {'lname': true}, {'country': false}];
Object.entries(arry.reduce((acc, val) => Object.assign({}, acc, val)))
.filter(([k, v]) => v)
.map(([k, v]) => k)