Array

获取 Array.prototype 的所有方法

1
Object.getOwnPropertyNames(Array.prototype)

改变自身值的方法(9个)

不会改变自身的方法(9个)

  • concat
  • join
  • slice
  • toString
  • toLocaleString
  • indexOf
  • lastIndexOf
  • includes(ES7)
  • toSource(非标准)

遍历方法

forEach

1
2
3
4
5
array.forEach(callback(currentValue, index, array){
//do something
}, this)

array.forEach(callback[, thisArg])

callback

  • 数组当前项的值
  • 数组当前项的索引
  • 数组对象本身

返回值
undefined.

1
2
3
4
let o = {0:'数组当前项的值', 1:'数组当前项的索引', 2:'数组对象本身', length:3};
Array.prototype.forEach.call(o,function(currentValue, index, array){
console.log(currentValue, index, array);
},o);

every

1
array.every(callback[, thisArg])

some

1
array.some(callback[, thisArg])

filter

1
var new_array = arr.filter(callback(element[, index[, array]])[, thisArg])

map

1
2
3
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// Return element for new_array }[,
thisArg])

reduce

1
arr.reduce(fn, initialValue)

callback

  • Accumulator (acc) (累计器)
  • Current Value (cur) (当前值)
  • Current Index (idx) (当前索引)
  • Source Array (src) (源数组)

initialValue 指定第一次调用 fn 的第一个参数。如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错

返回值
函数累计处理的结果

1
2
3
4
5
6
7
求字符串中字母出现的次数
const str = 'sfhjasfjgfasjuwqrqadqeiqsajsdaiwqdaklldflas-cmxzmnha';
const res = str.split('').reduce((accumulator, cur) => {
accumulator[cur] ? accumulator[cur]++ : accumulator[cur] = 1;
return accumulator;
}, {});
// {s: 6, f: 4, h: 2, j: 4, a: 8,…}

1
2
3
10为初始值求和
const arr = [1, 2, 3];
const sum = arr.reduce((total, num) => total + num, 10); // 16
1
2
3
数组最大值
const a = [23,123,342,12];
const max = a.reduce(function(pre,cur,inde,arr){return pre>cur?pre:cur;}); // 342

entries

1
arr.entries()

iterator.next()

返回值
返回一个新的Array Iterator对象,该对象包含数组中每个索引的键/值对。(http://www.ecma-international.org/ecma-262/6.0/#sec-createarrayiterator)是对象,它的原型(__proto__:Array Iterator)上有一个next方法,可用用于遍历迭代器取得原数组的[key,value]

1
2
3
4
5
6
7
// entries 也受益于鸭式辨型,如下:
var o = {0:"a", 1:"b", 2:"c", length:3};
var iterator = Array.prototype.entries.call(o);
console.log(iterator.next().value); // [0, "a"]
console.log(iterator.next().value); // [1, "b"]
console.log(iterator.next().value); // [2, "c"]
console.log(iterator.next().value); // undefined, 迭代器处于数组末尾时, 再迭代就会返回undefined

find

返回数组中找到的元素的值,而不是其索引

1
arr.find(callback[, thisArg])

keys

返回一个包含数组中每个索引键的Array Iterator对象。

返回值
一个新的 Array 迭代器对象

1
2
3
4
5
6
// 索引迭代器会包含那些没有对应元素的索引
var arr = ["a", , "c"];
var sparseKeys = Object.keys(arr);
var denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]

values

返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值

返回值
一个新的 Array 迭代器对象

1
2
3
4
5
6
7
let arr = ['w', 'y', 'k', 'o', 'p'];
let eArr = arr.values();
console.log(eArr.next().value); // w
console.log(eArr.next().value); // y
console.log(eArr.next().value); // k
console.log(eArr.next().value); // o
console.log(eArr.next().value); // p

js中reduce的神奇用法

JavaScript 标准库

JavaScript数组所有API全解密

30 seconds of code 中文版翻译