我收到了下一个JSON回复
{
"timetables":[
{"id":87,"content":"B","language":"English","code":"en"},
{"id":87,"content":"a","language":"Castellano","code":"es"}],
"id":6,
"address":"C/Maestro José"
}
我想实现下一个伪代码功能
for(var i in json) {
if(json[i] is Array) {
// Iterate the array and do stuff
} else {
// Do another thing
}
}
有什么想法吗?
答案 0 :(得分:46)
还有其他方法,但据我所知,这是最可靠的方法:
function isArray(what) {
return Object.prototype.toString.call(what) === '[object Array]';
}
因此,要将其应用于您的代码:
for(var i in json) {
if(isArray(json[i])) {
// Iterate the array and do stuff
} else {
// Do another thing
}
}
答案 1 :(得分:8)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if(Array.isArray(json[i])){
// true
...
}
答案 2 :(得分:4)
function isArray(ob) {
return ob.constructor === Array;
}