开发者

Difference between array and arrayvalue [duplicate]

开发者 https://www.devze.com 2023-04-10 10:33 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_如何学编程 Possible Duplicate:
This question already has answers here: Closed 11 years ago. 开发者_如何学编程

Possible Duplicate:

How do you check if a variable is an array in JavaScript?

How can I check if a variable is an array (so I can handle each arrayvalue) or a single arrayvalue?


From the MDN page for isArray which is part of the ECMAScript 5 standard:

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

In many cases, you can also just check to see if there is a .length property (this is often what jQuery does) because this will also accept any array-like object that isn't actually an array, but can be iterated like one. Obviously, if you have things that do have a .length property that you don't want treated like an array, this will get fooled by that situation so you have to know which you want:

function isArrayLike(item) {
    return(typeof item.length != "undefined");
}
0

精彩评论

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