-2

Need a specific array structure.

arr = [];
arr1 = {'a':12,'b':11};
arr2 = {'c':12,'d':12};
arr.push(arr1);
arr.push(arr2);`

with this i got the result

(2) [Object, Object]
0:
{
 a:12,
 b:11
}
1:
{
 c:12,
 d:12
}

But I need this response

[{'a':12,'b':11},{'c':12,'d':12}]
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • 2
    Looks fine to me, console.log(arr) gives the response you expect. – James May 24 '17 at 16:07
  • 2
    Those are two notations mean the same thing. How are you trying to use the response? – shotor May 24 '17 at 16:07
  • I don't understand the problem. You want an array with two objects. One with properties `a` and `b`. The other with properties `c` and `d`. That's what you're getting. – Mike Cluck May 24 '17 at 16:07
  • 0: and 1: are simply your console print telling you the index of those elements. – Taplar May 24 '17 at 16:08
  • 3
    As with many things in life, it's more important what's inside of `arr` (the value of `arr`), not what it *appears* to be from the outside (how the console displays the value). What you have is exactly what you want. – Felix Kling May 24 '17 at 16:13

1 Answers1

-1

All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:

JSON.stringify(arr); //"[{"a":12,"b":11},{"c":12,"d":12}]"
Erick Lanford Xenes
  • 1,467
  • 2
  • 20
  • 34