常见 js 数组方法使用详解

时间:2023-03-09 15:28:45
常见 js 数组方法使用详解

数组常用方法总结

  • concat
  • filter
  • map
  • some
  • every
  • reduce
  • sort
  • includes
  • join

some

every

语法:array.every(function(currentValue, index, arr){}, thisValue);

function isEvery(currentValue, index, arr) {
console.log(currentValue, index, arr);
return currentValue <= 7
}
var arr = [1,4,5,8,9];
arr.every(isEvery) // 1 0 [1, 4, 5, 8, 9]
// 4 1 [1, 4, 5, 8, 9]
// 5 2 [1, 4, 5, 8, 9]
// 8 3 [1, 4, 5, 8, 9]
// every 返回值 boolean,当发现数组中又不符合条件项,立即返回 false, 不在执行后续。否则将执行全部项。
function isEvery(currentValue, index, arr) {
console.log(this);
return currentValue <= 7
}
var arr = [1,4,5,8,9];
arr.every(isEvery, 33);
// 33
// thisValue 可以是任意类型值。指向 every callback 内部 this