NishanCK
NishanCK

Reputation: 57

Converting array of Javascript objects to a Javascript array

I have a javascript object and when I JSON.stringify it, it looks like following

[
    {
        "item_id": null,
        "parent_id": "none",
        "depth": 0,
        "left": "1",
        "right": 4
    },
    {
        "item_id": "1",
        "parent_id": null,
        "depth": 1,
        "left": 2,
        "right": 3
    }
]

I want to convert it to a multi-dimensional array which looks like the following

item[0][0] = item_id
item[0][1] = parent_id
item[0][2] = depth
item[0][3] = left
item[0][4] = right

item[1][0] = item_id
item[1][1] = parent_id
item[1][2] = depth
item[1][3] = left
item[1][4] = right

Any help will be much appreciated :)

Edit : Got it working with the help of all :) Thanks everybody for the help.

Upvotes: 0

Views: 104

Answers (4)

d.raev
d.raev

Reputation: 9546

With recursion, for not limited dimensions of the data object:

fiddle

 function isArray($obj) {
    return Object.prototype.toString.call($obj) === '[object Array]';
}

function subObject(data) {


    if(isArray(data)){
        return (subArray(data));
    }

    var result = [];
    for (var i in data) {
        var item = data[i];
        if (item === null) { // null type is ojbect ..!
            result.push(item);
        } else if (isArray(item)) {
            result.push(subArray(item));
        } else if (typeof item === 'object') {
            result.push(subObject(item));
        } else {
            result.push(item);
        }
    }
    return result;
}

function subArray(data) {
    var result = [];

    for (var i = 0; i < data.length; i++) {
        var item = data[i]; 
        if (item === null) { // null type is ojbect ..!
            result.push(item);
        } else if (isArray(item)) {
            result.push(subArray(item));
        } else if (typeof item === 'object') {
            result.push(subObject(item));
        } else {
            result.push(item);
        }
    }
    return result;
}

var r = subObject(data);
console.log(r);

Upvotes: -2

musefan
musefan

Reputation: 48415

Well lets take the initial object (array) prior to the stringify. With this we can loop each item. Then we can create a new array for each property. Something like this:

var myObject = X;//this is your original object
var newArray = [];

for(var i = 0; i < myObject.length; i++){
   var item = myObject[i];
   var subArray = [];
   subArray.push(item["item_id"]);
   subArray.push(item["parent_id"]);
   subArray.push(item["depth"]);
   subArray.push(item["left"]);
   subArray.push(item["right"]);
   newArray.push(subArray);
}

Here is a working example (check the console for the result)

NOTE: I purposefully avoided using a for in loop due to the rumours I always hear about the reliability of order. Of course, if you trust it then it's your call, but I prefer to play on the safe side. You can read some other opinions of this matter here.


If you want to increase the performance, you could create an array directly from the values, like so:

for (var i = 0; i < myObject.length; i++) {
    var item = myObject[i];
    var subArray = [item["item_id"], item["parent_id"], item["depth"], item["left"], item["right"]];
    newArray.push(subArray);
}

This is approximately twice as fast performance wise, here is the proof

Upvotes: 4

PA.
PA.

Reputation: 29339

map your array elements by iterating over the fields of each of the objects

var item = yourarray.map(function (o) { 
  var s =[];
  for (var f in o) { 
    s.push(o[f]);
  }
  return s; 
});

Upvotes: 0

Joe Beuckman
Joe Beuckman

Reputation: 2296

Your "object" is actually an Array.

var item = [];
for (var i=0; i<yourArray.length; i++) {
   var subArray = [];
   var obj = yourArray[i];
   for (var j in obj) {
      subArray.push(j);
   }
   item.push(subArray);
}

Upvotes: 1

Related Questions