I have a function which returns an array which has filter, sort and map. How can I push a new item into the array before its sorted?
When I try the following I get an error saying push isnt a function.
return (
myArray
.filter((item) => {
return item.value > 2;
})
.push(newThiny) // This doesn't work
.sort((a, b) => {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
})
.map((item) => {
return (
<ChooseAnExercises
key={item.name}
name={item.name}
active={item.active}
setNumber={this.props.number}
updateValue={this.props.updateValue}
/>
)
})
)
.push(newThiny)
will return a number
so .sort
function will throw an error, since it doesn't work on numbers
, only on arrays.
I would suggest you to use .concat
instead.
.concat(newThiny).sort({ ... })
.push(newThiny) // This doesn't work
Push returns the length of the array rather than the array itself.
Rather than push try concat
.concat([newThiny])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With