Sometimes, we want to fix the issue when we call JavaScript array push with multiple objects and gets undefined.
In this article, we’ll look at how to fix the issue when we call JavaScript array push with multiple objects and gets undefined.
How to fix the issue when we call JavaScript array push with multiple objects and gets undefined?
To fix the issue when we call JavaScript array push with multiple objects and gets undefined, we should get the latest value of the array we call push
on.
For instance, we write:
const arr = [1, 2, 3]
arr.push(...[4, 5, 6])
console.log(arr)
to call arr.push
with the [4, 5, 6]
spread as the arguments of push
with the spread operator.
Therefore, from the console log, arr
is [1, 2, 3, 4, 5, 6]
since push
appends the values into arr
in place.
Conclusion
To fix the issue when we call JavaScript array push with multiple objects and gets undefined, we should get the latest value of the array we call push
on.