Categories
JavaScript Answers

How to array push key value with JavaScript?

Spread the love

To array push key value with JavaScript, we call the push method.

For instance, we write

const arr = ["left", "top"];
const x = [];

for (const a of arr) {
  x.push({
    [a]: 0,
  });
}

to loop through the arr array with a for-of loop.

Then we call x.push with an object with key a and value 0.

a is the value being looped through in arr.

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 *