I'm aware of similar questions such as here but I can't figure out what should be an easy problem. I'm trying to filter an object of objects for truthy values.
const obj = {
piano: {
scales: { essential: 1 },
chords: { essential: 1 },
triads: { essential: 0 },
},
bass: { scales: { essential: 1 }, triads: { essential: 0 } },
};
function getEssential(state) {
return Object.fromEntries(
Object.entries(state).filter(([, val]) => val.essential)
);
}
const resOne = Object.fromEntries(
Object.entries(obj.piano).filter(([, val]) => val.essential)
);
const resTwo = Object.keys(obj).forEach((el) =>
Object.entries(obj[el]).filter(([, val]) => val.essential)
);
const resThree = Object.keys(obj).forEach((el) => getEssential(el));
console.log(resOne);
console.log(resTwo);
console.log(resThree);
See that resOne gives desired output but for only one key (piano in this case). I know I can make an empty array and add the ones that pass the test but there must be a more efficient way.
How do I take an object, loop through all keys, apply the filter and return the same object minus the properties essential !== 1?
Current output:
{ scales: { essential: 1 }, chords: { essential: 1 } }
undefined
undefined