Isaac Chambers Isaac Chambers - 1 year ago 98
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download