Javascript object for array

I am having trouble converting some JSON and I will help. This is my problem, I got the following JSON:

Example of received JSON (from 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 to this will ONLY accept a multidimensional array. I can not do anything. I was browsing web pages 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 continues to get stuck in the "no object length" part. When I repeat every object, I get a character by character. Unfortunately, these field names (rating, title, author), etc. Not set in stone, and I cannot access anything using the obj.Field notation.

; JSON?

+3
4

JavaScript Undersore.js, /.

, ,

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

, , .

, object.member object["member"]. , , .

for-in (. 12.6.4). .

, innerArray ,

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

, , json-.

+4

This will output:

var newJSON = [];

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

AT...

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

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

0
source

There is an easy way to do this.

With a one-dimensional object:

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

If you have a multi-dimensional object, you can use the same ideas using a recursive function or loop, checking the type of element.

0
source

All Articles