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"] ... ]
11
  • 3
    Did you try just JSON.stringify(datas) ? Commented Apr 28, 2015 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? Commented Apr 28, 2015 at 16:41
  • Also update is not a function how should I merge the new object once it is created in the loop? Commented Apr 28, 2015 at 16:42
  • Can you show an example of the source and the final result? Commented Apr 28, 2015 at 16:43
  • What is your desired output? Commented Apr 28, 2015 at 16:49

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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.