Categories
JavaScript Answers

How to do the equivalent of array push() method with objects with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *