Dynamically I am getting an array
.
For example we can consider this following array
.
var sampleArray=[
"logo",
"Details",
"titles"
];
But I want it something like this.
jsonObj={
"poistion1":"logo",
"poistion2":"Details",
"poistion3":"titles"
}
You can iterate on array and create object like following
var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
jsonObj["position" + (i+1)] = sampleArray[i];
}
Like this
var jsonObj = {};
var sampleArray = [
"logo",
"Details",
"titles"
];
for (var i = 0, len = sampleArray.length; i < len; i++) {
jsonObj['position' + (i + 1)] = sampleArray[i];
}
console.log(jsonObj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With