Trying to make a function that uses filter but not a for or while loop or foreach function, that will loop through an array of objects only to return their property values.
In case someone is looking for a single pass through it can be done with .reduce()
So instead of doing:
const getShortMessages = (messages) => messages
.filter(obj => obj.message.length <= 50)
.map(obj => obj.message);
You can do:
const getShortMessages = (messages) => {
return messages.reduce((shortMessages, i) => {
if (i.message.length <= 50) {
shortMessages.push(i.message)
}
return shortMessages
},[])
}
Or if you want even shorter hand:
const getShortMessages = (messages) => messages.reduce((shortMessages, item) => (item.message.length <= 50)
? shortMessages=[...shortMessages,item.message] : shortMessages,[])