Isaac Chambers Isaac Chambers - 2 months ago 7
Javascript Question

How can I generate arbitrary object property names in JavaScript?

I am looking for a way of creating a JavaScript object with varying property names. Something like this.

Imagine me getting the values from an array

var arraywithpropValues = ["property=propertyValue, value=value"];


I want to create an object like

Object {property: "propertyValue", value: "value"}


Remember the property and value in the array can change anytime.

Is this possible?

Answer

Objects are associative arrays. You can assign to a variable property name like so:

var name  = "foo";
var value = "bar";

object[name] = value;

I'll leave the string parsing of arraywithpropValues to you.