Isaac Chambers Isaac Chambers - 1 month ago 5x
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?


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.