Sometimes, we want to do the equivalent of array push() method with objects with JavaScript.
In this article, we’ll look at how to do the equivalent of array push() method with objects with JavaScript.
How to do the equivalent of array push() method with objects with JavaScript?
To do the equivalent of array push() method with objects with JavaScript, we can add properties to objects with assignment.
For instance, we write
const tempData = {};
for (const [index, d] of data.entries()) {
if (d.status === "Valid") {
tempData[index] = d;
}
}
to create the tempData
object
Then we call data.entries
to return an array with key-value pair entries from the data
array.
And then we use a for-of loop to check the d.status
property value.
And if it’s 'Valid'
, then we add the index
property to tempData
and assign d
to it.
Conclusion
To do the equivalent of array push() method with objects with JavaScript, we can add properties to objects with assignment.