How to turn an array into an object with two values? javascript -
i want put array object 2 keys (key, val). code.
var arr = ["hello", "44", "thanks", "32"]; console.log(arr); console.log(arr.length); var obj = {}; (var = 0; < arr.length; i++) { obj.key = arr[i]; } console.log(obj);
this result have.
obj[0] = {key: "hello", val: "44"}; obj[1] = {key: "thanks", val: "32"};
thanks allot!
so, want loop every 2 item instead of 1, , take current item , next one.
maybe :
obj = []; (var = 0; < arr.length; i=i+2) { obj.push({key:arr[i], val:arr[i+1]}); }
Comments
Post a Comment