dmikester1
dmikester110mo ago

✅ – ✅ – ✅ – 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
Jump to solution
14 Replies
dmikester1
dmikester110mo ago
Maybe I should change my objects to look like: {type: 'standard', value: true} ?
Solution
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
reactibot
reactibot10mo ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1155927650322366534 This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1155927650322366534
dmikester1
dmikester110mo ago
Makes sense, thank you!
ScriptyChris
ScriptyChris10mo 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)
dmikester1
dmikester110mo ago
aw, ok, what does 'length' do?
ScriptyChris
ScriptyChris10mo 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
dmikester1
dmikester110mo ago
oh interesting, that is cool way to do it! Never would have thought of that, thanks!
reactibot
reactibot10mo ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1155927650322366534
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
ScriptyChris
ScriptyChris10mo 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
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
ScriptyChris
ScriptyChris10mo ago
Okay, fair point. I've hoped that was only an example and final data is bigger 😅
reactibot
reactibot10mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1155927650322366534