我收到了下一个JSON响应
{
"timetables":[
{"id":87,"content":"B","language":"English","code":"en"},
{"id":87,"content":"a","language":"Castellano","code":"es"}],
"id":6,
"address":"C/Maestro José"
}
Run Code Online (Sandbox Code Playgroud)
我想实现下一个伪代码功能
for(var i in json) {
if(json[i] is Array) {
// Iterate the array and do stuff
} else {
// Do another thing
}
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
Jam*_*mes 50
还有其他方法,但据我所知,这是最可靠的:
function isArray(what) {
return Object.prototype.toString.call(what) === '[object Array]';
}
Run Code Online (Sandbox Code Playgroud)
因此,要将其应用于您的代码:
for(var i in json) {
if(isArray(json[i])) {
// Iterate the array and do stuff
} else {
// Do another thing
}
}
Run Code Online (Sandbox Code Playgroud)
son*_*chy 13
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if(Array.isArray(json[i])){
// true
...
}
Run Code Online (Sandbox Code Playgroud)
function isArray(ob) {
return ob.constructor === Array;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45363 次 |
| 最近记录: |