Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push an object with a variable as a property name onto an array [duplicate]

I have a javascript array and a variable. I want to push onto the array an object with the variable as the property name, but it must hold the value of the variable, not the variable as a string. If I have this code:

array = [];
var x = 10;
array.push({x: y});

The x is stored as a string "x", not a variable that contains the value of var x. Any help is appreciated.

like image 772
Ben Avatar asked Jun 13 '14 04:06

Ben


People also ask

How do you push an object to an existing array?

The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the end of the array.

Does Push create a copy?

So answering above question, It depends upon what you're pushing. Objects and arrays are pushed as a pointer to the original object. Built-in primitive types like numbers or booleans are pushed as a copy.

How do you push an object into an array in react?

Use the spread syntax to push an element into a state array in React, e.g. setNames(current => [... current, 'Carl']) . The spread syntax (...) will unpack the existing elements of the state array into a new array where we can add other elements.

How to push an object to an array in JavaScript?

Arrays of objects in JavaScript is a powerful feature because of how much information you can store all in one variable. In this post, we're going to learn how you can push an object to an array in JavaScript. When you have an an array of objects and want to push another object to the end of the array, you can use the push () method.

How to add a variable as the name of a property?

The name: values pairs in JavaScript objects are called properties. We can add the property to JavaScript object using a variable as the name by using dot notation or bracket notation. Below example illustrate explain two different approaches: Example 1: In this example, we will be using dot notation.

How to add a property to a JavaScript object using variable?

Now at it's lowest price ever! The name: values pairs in JavaScript objects are called properties. We can add the property to JavaScript object using a variable as the name by using dot notation or bracket notation. Example 1: In this example, we will be using dot notation.

How to name values pairs in JavaScript objects using variables?

The name: values pairs in JavaScript objects are called properties. We can add the property to JavaScript object using a variable as the name by using dot notation or bracket notation. Example 1: In this example, we will be using dot notation.


1 Answers

The property name in an object literal is not evaluated as a variable. You have to assign the property using bracket notation.

var obj = {};
obj[x] = y;
array.push(obj);
like image 70
Barmar Avatar answered Sep 28 '22 15:09

Barmar