开发者

Javascript Object to Array

开发者 https://www.devze.com 2023-03-09 20:17 出处:网络
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:

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?


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


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.


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/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消