Speedy
Speedy

Reputation: 256

Javascript Object to Array

I'm having some problems with converting some JSON and I'm after some help. This is my problem, I have JSON returned the following:

Example of the JSON recieved (from a CSV file):

[
    {
        "rating": "0",
        "title": "The Killing Kind",
        "author": "John Connolly",
        "type": "Book",
        "asin": "0340771224",
        "tags": "",
        "review": "i still haven't had time to read this one..."
    },
    {
        "rating": "0",
        "title": "The Third Secret",
        "author": "Steve Berry",
        "type": "Book",
        "asin": "0340899263",
        "tags": "",
        "review": "need to find time to read this book"
    },

    cut for brevity   

]

Now, this is a one dimensional array of objects, but I have a function that I need to pass this to that will ONLY take a multidimensional array. There's nothing I can change on this. I've been looking around the web for conversion and came across this code:

if (! obj.length) { return [];} // length must be set on the object, or it is not iterable  
   var a = [];  

   try {  
       a = Array.prototype.slice.call(obj, n);  
   }  
   // IE 6 and posssibly other browsers will throw an exception, so catch it and use brute force  
   catch(e) {  
       Core.batch(obj, function(o, i) {  
           if (n <= i) {  
               a[i - n] = o;  
           }  
       });  
   }  

   return a;  

But my code keeps getting stuck on the "no object length" part. When I iterate through each object, I get character by character. Unfortunately, those field names (rating, title, author), etc are not set in stone and I can't access anything using the obj.Field notation.

I'm stuck on this; is there a way to convert those objects into arrays, or do I have to go back to the beginning and dump JSON?

Upvotes: 3

Views: 15546

Answers (4)

Idealmind
Idealmind

Reputation: 1298

There is is a simple way to do it

With a unidimensional object:

var myarrayfromobject = new Array();
$H(myobject).each(function(item, i){
    myarrayfromobject[i] = item;
});

If you have a multidimensional object, you can use te same idea, using a recursive function or a loop, verifying the type of item.

Upvotes: 0

mkilmanas
mkilmanas

Reputation: 3485

There is a nice JavaScript library called Undersore.js which does all kind of object/array manipulations.

Using it you could do the conversion as easy as this

_(json).each(function(elem, key){
    json[key] = _(elem).values();
});

Upvotes: 4

Gary Green
Gary Green

Reputation: 22395

This will output:

var newJSON = [];

for (var i = 0, len = json.length; i < len; i++)
{
    newJSON.push([json[i]]);
}

Into...

[
 [Object { rating="0", title="The Killing Kind", more...}],
 [Object { rating="0", title="The Third Secret", more...}]
]

Fiddle: http://jsfiddle.net/Ta6hW/

Upvotes: 0

Paul Butcher
Paul Butcher

Reputation: 6956

If I understand you correctly, there are two things you need to know to achieve this.

One is the fact that object.member can also be written as object["member"]. This means that you do not have to hard-code your property names into your object to array translator, but can treat them as strings.

The second is the for-in statement (see section 12.6.4). For an object, this loops through all of its members.

The following will result in innerArray being an array containing all of the values of all the members of the object

var innerArray = [];
for (property in object) {
    innerArray.push(object[property]);
}

Using this knowledge, you can tailor the array to contain whatever information it is you need to extract from the json objects.

Upvotes: 4

Related Questions