✅ – ✅ – ✅ – dmikester1 – 18-03 Sep 25

Is there an easy way to filter this array by truthy values?
const typesArr = [
{ standard: true },
{ "major-retail": true },
{ "minor-retail": false },
{ mini: false },
{ "greens-grade": false },
{ "ice-melt": true }
];
const typesArr = [
{ standard: true },
{ "major-retail": true },
{ "minor-retail": false },
{ mini: false },
{ "greens-grade": false },
{ "ice-melt": true }
];
Solution:
Message Not Public
Sign In & Join Server To View
D
dmikester1205d ago
Maybe I should change my objects to look like: {type: 'standard', value: true} ?
UU
Unknown User205d ago
D
dmikester1205d ago
Makes sense, thank you!
S
ScriptyChris205d ago
Like that typesArr.filter(Boolean)? Oh, wait there are objects
typesArr.filter(obj => Object.values(obj).filter(Boolean).length)
typesArr.filter(obj => Object.values(obj).filter(Boolean).length)
D
dmikester1205d ago
aw, ok, what does 'length' do?
S
ScriptyChris205d ago
length prop returns the length of array and if it is empty, length returns 0 and filter would skip that element from output array
D
dmikester1205d ago
oh interesting, that is cool way to do it! Never would have thought of that, thanks!
UU
Unknown User205d ago
S
ScriptyChris205d ago
Why only check for 1 prop, when there is no certainty that other props may be truthy/falsy 🤔 I mean, it depend on the input structure
UU
Unknown User205d ago
S
ScriptyChris205d ago
Okay, fair point. I've hoped that was only an example and final data is bigger 😅
UU
Unknown User204d ago