This is my JSON
with Javascript
:
/**
* @param {String} value1
* @param {String} value2
* @param {String} value3
* @param {Number} value4
*
* @constructor
* @properties={typeid:24,uuid:"F146465E-7D8C-4D8B-B37C-954E65AFBEAD"}
*/
function CarData (value1, value2, value3, value4)
{
this.car = {
field1: value1,
field2: value2,
field3: value3,
field4: value4
}
}
I want to do that the fieldX
be dynamic like the value passed with parameters.
Is that possible?
Then I will transform that object to JSON with:
JSON.stringify(object);
Yes, you can assign dynamic field values using the []
operator.
Example:
this.car = {}; // this just creates an empty object
this.car['typeid'] = 24; // same as this.car.typeid = ..
for(var i = 0; i < 4; i++) {
this.car['part' + i] = parts[i]; // assign to dynamic fieldname 'partX'
}