Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  171] [ 3]  / answers: 1 / hits: 68052  / 15 Years ago, thu, june 4, 2009, 12:00:00

I am receiving the next JSON response



    {
timetables:[
{id:87,content:B,language:English,code:en},
{id:87,content:a,language:Castellano,code:es}],
id:6,
address:C/Maestro José
}


I would like to achieve the next pseudo code functionality



for(var i in json) {            
if(json[i] is Array) {
// Iterate the array and do stuff
} else {
// Do another thing
}
}


Any idea?


More From » json

 Answers
39

There are other methods but, to my knowledge, this is the most reliable:



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


So, to apply it to your code:



for(var i in json) {                    
if(isArray(json[i])) {
// Iterate the array and do stuff
} else {
// Do another thing
}
}

[#99380] Tuesday, June 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aleighabayleef

Total Points: 511
Total Questions: 99
Total Answers: 99

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
aleighabayleef questions
Fri, Jun 5, 20, 00:00, 4 Years ago
Thu, Jun 4, 20, 00:00, 4 Years ago
Sat, Sep 21, 19, 00:00, 5 Years ago
Thu, Jul 18, 19, 00:00, 5 Years ago
;