What's the most readable way to write nested functions in javascript?
Seems honest to me. What you can do is replace "50" with a variable in your js file.
var MESSAGE_MAX_LENGTH= 50;
And re-place the bit the way you are addressing the function
function getShortMessages(messages) {
return messages
.filter( (messageObject) => {
return messageObject.message.length < MESSAGE_MAX_LENGTH
})
.map( (messageObject) => {
return messageObject.message
});
}
Also I found that when accessing an array messages
and running a function, filter
it is better not to call the object messages
, but item
ormessageObject
Also, object
in a function map
is a little sinister, call it messageObject
again, for example, so you know what exactly you are using
source to share
in ES 6 you can use shortcuts like:
function getShortMessages(messages) {
return messages.filter( messages => messages.message.length < 50).map( object => object.message )
}
Its up to you who we read. On one line, you don't need to use {}
and return
, and if you are using an array function, with 1 parameter you also don't need to (messages) =>
, you can only usemessages =>