1
var temparray1 = [[1,3,4],[5,6,7],[8,9,10]];
var final = [];
var obj = {};
for(var temp in temparray1){
    for(var test in temparray1[temp]){
        obj.b = temparray1[temp][0];
        obj.c = temparray1[temp][1];
        obj.d = temparray1[temp][2];
    }
    console.log(obj);
    final.push(obj);
}

current output

[{ b: 8, c: 9, d: 10 }
{ b: 8, c: 9, d: 10 }
{ b: 8, c: 9, d: 10 }]

expected Out put:

[{ b: 1, c: 3, d: 4 }
{ b: 5, c: 6, d: 7 }
{ b: 8, c: 9, d: 10 }]

i am running my javascript in node.js -v 8.1.x server

in the console at the end of the for loop it prints the required out put but not in the array push

2
  • 2
    Never use for ..in loops to iterate over arrays, and obj isn't defined in your code ? Commented Aug 8, 2017 at 16:09
  • yes obj = {} already declared but may i know why for ..in is not possible it is the normal iteration i think Commented Aug 9, 2017 at 8:50

4 Answers 4

1

var temparray = [[1, 3, 4], [5, 6, 7], [8, 9, 10]];
const final = temparray.map(a => ({ b: a[0], c: a[1], d: a[2] }));

console.log(final);

Sign up to request clarification or add additional context in comments.

Comments

1

Probably, you set obj outside the for loop, therefore the props are overwritten and you push the same object multiple times into the array. Simply move the obj declaration into the loop. And you probably just need the outer loop.

Btw much shorter:

let final = temparray1.map(
 ([b,c,d]) => ({b,c,d})
);

Comments

1

You could use Array#map and return an object with the wanted properties.

var array = [[1, 3, 4], [5, 6, 7], [8, 9, 10]],
    result = array.map(function (a) {
        return { b: a[0], c: a[1], d: a[2] };
    });
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

With ES6, you could map the inner arrays as well with an array for the keys with Object.assign and spread syntax ....

var array = [[1, 3, 4], [5, 6, 7], [8, 9, 10]],
    keys = ['b', 'c', 'd'],
    result = array.map(a => Object.assign(...keys.map((k, i) => ({ [k]: a[i] }))));
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

Here is what you wanted

var temparray1 = [[1,3,4],[5,6,7],[8,9,10]];
var final = [];
for(var temp in temparray1){
   var obj = {};
   obj['b'] = temparray1[temp][0];
   obj['c'] = temparray1[temp][1];
   obj['d'] = temparray1[temp][2];
   final.push(obj);
}
console.log(final);

Hope this helps!

8 Comments

what errors are you getting ? I can send you a live example for it, if you want it
the above code push only the last occurene of array [{ b: 8, c: 9, d: 10 } { b: 8, c: 9, d: 10 } { b: 8, c: 9, d: 10 }] @zenwraight array.map function is the correct apporach
I have posted the link of my live example above @muthukumar, please run it and see for yourself
Thank you @zenwraight it worked for me i have changed your array to objects really nice and great ` var obj = {};`
What issue you are facing @Jonasw, it's is one the perfect answer for the above question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.