Using below code I'm attempting to convert an array of variable length to an object where item at position i is object name and item at position i+1 is item value. For example :
arr = [];
arr.push('a');
arr.push(1);
arr.push('b');
arr.push(2);
arr.push('c');
arr.push(3);
/* Want to create an object that is of type : */
var ob = {a:1 , b:2 , c:3}
console.log(ob)
Here is code I'm trying to achieve this with :
var ob2 = {}
for (var i = 0; i < arr.length; i++) {
ob2.arr[i] = arr[i + 1]
i = i + 1
}
But receive error :
(index):63 Uncaught TypeError: Cannot set property '0' of undefined
at window.onload ((index):63)
fiddle : https://jsfiddle.net/wxkkjzm0/
arr = [];
arr.push('a');
arr.push(1);
arr.push('b');
arr.push(2);
arr.push('c');
arr.push(3);
/* Want to create an object that is of type : */
var ob = {a:1 , b:2 , c:3}
console.log(ob)
var ob2 = {}
for (var i = 0; i < arr.length; i++) {
ob2.arr[i] = arr[i + 1]
i = i + 1
}
console.log(ob2);
arr & ob2 are defined which this error pertains to ?