-3

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 ?

zero298
  • 25,467
  • 10
  • 75
  • 100
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • Possible duplicate of [Convert Array to Object](https://stackoverflow.com/questions/4215737/convert-array-to-object) – zero298 Jan 10 '18 at 19:30
  • Why are you incrementing `i` twice? – SLaks Jan 10 '18 at 19:31
  • Obviously to only iterate over every 2nd element of the array. – connexo Jan 10 '18 at 19:33
  • `ob2` has no property `arr` because you **just** defined it to `{}` but you are trying to reference properties on it with `ob2.arr[i]` – zero298 Jan 10 '18 at 19:33
  • 1
    `arr` is literal in `obj.arr[i]`. You want to retrieve the value from the `arr[i]` (ie. dynamic), so you need square brackets: `ob2[arr[i]]`. – trincot Jan 10 '18 at 19:37
  • Possible duplicate of [JavaScript - cannot set property of undefined](https://stackoverflow.com/questions/7479520/javascript-cannot-set-property-of-undefined) – devlin carnate Jan 10 '18 at 19:47
  • `for (let i = 0; i < arr.length; i = i + 2) ob2[arr[i]] = arr[i + 1]` – dfsq Jan 10 '18 at 20:10

5 Answers5

5

You could increment the index variable by 2, because you need a pair for assignment a key/value pair.

var array = ['a', 1, 'b', 2, 'c', 3],
    object = {},
    i

for (var i = 0; i < array.length; i += 2) {
    object[array[i]] = array[i + 1];
}

console.log(object);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

As the error is trying to tell you, ob2.arr doesn't exist.

You probably want the property with that name, which would be

ob2[arr[i]]
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

your error is with:

ob2.arr...

and there's no need to do:

i = i + 1

since you're incrementing i in the for loop delcration (i++)

you want:

ob2[arr[i]] = arr[i+1]

note that you will go out of bounds if you leave that as is so do:

for (let i = 1; i < arr.length; i+=2) {
   ob2[arr[i-1]] = arr[i]
}
mad.meesh
  • 2,558
  • 1
  • 13
  • 20
0

Destructive while loop approach

var arr = ['a', 1, 'b', 2, 'c', 3],
    ob={};

while(arr.length ){
  ob[arr.shift()] = arr.shift();
}

console.log(ob)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Since you are sure that your paired value match index and index + 1, try this code:

arr = []; 
arr.push('a');
arr.push(1);
arr.push('b');
arr.push(2);
arr.push('c');
arr.push(3);

console.log(arr);

var ob2 = {};

for (var i = 0; i < arr.length/2; i++) {
    ob2[arr[i*2]] = arr[(i*2) + 1];
}

console.log(ob2);

// output : {a:1 , b:2 , c:3};

The key is to divide iteration length by 2, and for each index, use array value of that index as new index value for object, and multiply index by 2 and + the result by one to get array value for next index.

KeitelDOG
  • 4,750
  • 4
  • 18
  • 33