let arr = [1,55,20,11,5,3,31];
console.log(arr.sort()); //[1, 11, 20, 3, 31, 5, 55],string排序
let arr = [1,55,20,11,5,3,31];
console.log(arr.sort((a,b)=>a-b)); //[1, 3, 5, 11, 20, 31, 55],number排序
function arrayHasOwnIndex(array, prop) {
return array.hasOwnProperty(prop) &&
/^0$|^[1-9]\d*$/.test(prop) &&
prop <= 4294967294; // 2^32 - 2
}
for (let key in arr) {
if (arrayHasOwnIndex(arr, key)) {
console.log(arr[key]);
}
}
- forEach() 为数组中含有有效值的每一项执行一次 callback 函数
var arr = [
{a:1},
{b:2},
{c:3}
];
function foo(json) {
//...
};
arr.forEach(foo); // 把数组的每一项作为参数,调用函数