0

I have a two dimensional array, datas, that I want to convert to an array of objects.

The keys are in datas[0], I want to extract them, name, child, and size. and then append each attribute to it to get a master object. For some reason it overrides and is only showing one object when I try this?

var test = new Object();
for (i = 0; i < datas.length; i++){
    var obj = new Object();
    obj.name = datas[i][0];
    obj.parent = datas[i][1];
    obj.size = datas[i][2];
    test.update(obj);
}

I would like the final result to be:

[
    {"name": "Billy", "parent":"Ann", "size": "1"},
    {"name": "Ben", "parent": "John", "size": "1"},
     etc...
]

The datas array looks like:

 [["Name", "Parent", "Size"], ["Billy", "Ann", "1"], ["Ben", "John", "1"] ... ]
doelleri
  • 19,232
  • 5
  • 61
  • 65
Ben
  • 13
  • 5
  • 3
    Did you try just `JSON.stringify(datas)` ? – adeneo Apr 28 '15 at 16:40
  • 1
    @adeneo that returns a a massive string with no key/pairs, I need each array to be an own object I guess? – Ben Apr 28 '15 at 16:41
  • Also update is not a function how should I merge the new object once it is created in the loop? – Ben Apr 28 '15 at 16:42
  • Can you show an example of the source and the final result? – Teemu Apr 28 '15 at 16:43
  • What is your desired output? – Chris Apr 28 '15 at 16:49
  • JSON **is** generally a string, and most of time a massive one, there are no keys or values to be accessed in a JSON string? Do you really mean that you're just trying to convert an array to an object ? – adeneo Apr 28 '15 at 16:52
  • Yes I am trying to convert multiple arrays to different objects.. – Ben Apr 28 '15 at 16:53
  • what does the `datas` array look like? Also, what is it that you expect `test.update` to do? – lemieuxster Apr 28 '15 at 16:58
  • I want to make an array of objects I guess. One array and within it all of the objects of each array. – Ben Apr 28 '15 at 17:04
  • possible duplicate of [Convert Array to Object](http://stackoverflow.com/questions/4215737/convert-array-to-object) – Jason Cust Apr 28 '15 at 17:27
  • I've updated your question to reflect that it doesn't actually have anything to do with JSON. I've also tried to state your problem a little more clearly and add a bit of formatting. – doelleri Apr 30 '15 at 17:30

2 Answers2

0

You can't make an object without properties, so your desired result can't be achieved.

Assuming you want:

[
    {"name": "Billy", "parent": "Ann", "size": "1"},
    {"name": "Ben", "parent": "John", "size": "1"},
     etc...
]

Try:

var test = [];
for(i = 0; i < datas.length; i++){
  test.push({
    name: datas[i][0],
    parent: datas[i][1],
    size: datas[i][2]
  });
}
// do something with test
James
  • 20,957
  • 5
  • 26
  • 41
0
{
  {"name": "Billy", "parent":"Ann", "size"="1"},
  {"name": "Ben", "parent": "John", "size" = "1"}
}

is not correct json. curly braces mean - object, object must be presented in key:value form. There are two possible correct json of this kind:

array of objects

[
  {"name": "Billy", "parent":"Ann", "size"="1"},
  {"name": "Ben", "parent": "John", "size" = "1"}
]

deep structure

{
  "Billy" {"parent":"Ann", "size"="1"},
  "Ben" {"parent": "John", "size" = "1"}
}

to generate first variant

    var res = []
    for(i = 0; i<datas.length; i++){
      var obj = new Object();
      obj.name = datas[i][0];
      obj.parent = datas[i][1];
      obj.size = datas[i][2];
      res.push(obj);
    } 
    JSON.stringify(res);

to generate second variant

    var res = new Object();
    for(i = 0; i<datas.length; i++){
      var obj = new Object();
      obj.parent = datas[i][1];
      obj.size = datas[i][2];
      res.[datas[i][0]] = obj;
    } 
    JSON.stringify(res);
skazska
  • 421
  • 2
  • 8