Array 方法总结

352 阅读3分钟

Array 总结

Array方法众多,在这里分门别类,按照不同的类型总结一下,加深自己的印象。

1.检测某个对象是不是数组

  value instanceof Array
  Array.isArray()

2.栈方法--(理解为数组末尾的操作方法)(数组的栈方法,会改变原数组)

  • push() 向栈顶添加元素(数组尾部增加)
  • pop() 删除栈顶元素(数组尾部删除)

push方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度

push 是可以返回数组长度的

  const animals = ['pigs', 'sheeps', 'dogs'];
  // push 返回的是该数组的新长度,这个以前没注意到,
  const count = animals.push('cows');
  console.log(count);

pop()方法,删除数组中的最后一个元素,并返回该元素的值。

  const plants = ['name', 'anto', 'kobne'];
  const res = plants.pop();
  console.log(res)

3.队列方法(数组前端的操作方法)(数组的队列方法,会改变原数组)

  • shift() (数组顶部删除)
  • unshift() (数组顶部增加)

shift() 方法,删除数组中的第一个元素,并返回该元素的值

  const array = [1, 2, 3]
  const res = array.shift();
  console.log(res)

unshift() 方法,增加元素到数组开头,并返回数组长度.

  const array = [1, 2, 3];

  const res = array.unshift('1', '2', '3');
  console.log(array)

4.重排序方法

  • reverse() (颠倒位置), 改变原数组
  • sort() 按照指定的函数进行排序,并返回数组

reverse() 方法,将数组中的元素位置颠倒,并返回数组,数组的第一个元素会变成最后一个,会改变数组。

const arr = [
  {a: 1},
  {b: 1},
  {c: 1},
]
const new_arr = arr.reverse()
console.log(new_arr)

sort(compareFunction) 方法,数组中的元素,按照原地算法对数组进行排序

function compare(value1, value2) {
  if (value1 < value2) {
    return 1
  } else if (value1 > value2) {
    return -1
  } else {
    return 0
  }
}
let arr = [1, 2, 3];
const res = arr.sort(compare);

// 如果 compareFunction(a, b) < 0, 那么a 排在b 之前,
// 如果 compateFunction(a, b) = 0, 那么a, b位置不变,
// 如果 compareFunction(a, b) > 0, 那么b,在 a之前。
console.log(res)

5.操作方法 (不会改变原数组)

  • concat()方法 (合并数组,不会改变原数组,返回新数组)
  • slice()方法 提取数组元素
  • splice() 修改数组元素

concat(),合并两个或多个数组。

  const array1 = [1.2,3];
  const array2 = [4,5,6];
  const res = array1.concat(array2,[7,8])
  // res [1,2,3,4,5,6,7,8]

slice(begin, end),提取指定位置的元素。

(提取包含begin,不包含end位置的元素) (如果不传入end,就一直提取到末尾位置)

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
console.log(animals.slice(1, 2))

splice() 可以删除,替换,添加元素来修改数组,并返回修改后的数组内容,这个方法会改变原数组。

这里的参数是个人理解的名称。

const months = ['Jan', 'March', 'April', 'June'];

// 三个参数的含义 index
months.splice(1, 0, 'feb');

/**
 * @insertIndex 想要插入元素的位置,
 * @replaceLength 要替换多少个元素
 * @items 想要增加的元素
 * /
months.splice(insertIndex, repalceLenth, items)

5.位置方法

  • indexOf() 从前往后查找, 若是能够找到,返回下标,若是没找到,返回-1
  • lastIndexOf() 从后往前查找,若是能够找到,返回下标,若是没找到,返回-1

6.迭代方法

  • map()
  • every() 数组中的每一项运行指定的函数,每一项返回true, 才会返回 true.
  • some() 数组中的每一项运行指定的函数,只要有一项为true, 则返回true。
  • forEach() 数组中的每一项运行指定函数。
  • filter() 过滤符合指定函数返回true 的数组。
let numbers = [0, 1, 2, 3, 5, 6];
const res = numbers.every(item => item > 2);
console.log(res) // false
let numbers = [0, 1, 2, 3, 5, 6];
const res = numbers.some(item => item > 2);
console.log(res) // true
let numbers = [0, 1, 2, 3, 5, 6];
const res = numbers.filter(item => item > 2);
console.log(res) // [ 3, 5, 6 ]
let numbers = [0, 1, 2, 3, 5, 6];
const res = numbers.map(item => item * 2);
console.log(res) // [ 0, 2, 4, 6, 10, 12 ]

7.归并方法 迭代数组中的所有项,然后构建一个最终返回的值。

  • reduce()
  • reduceRight()
let numbers = [1, 2, 3, 4, 5];
const res = numbers.reduce((prev, next, index) => {
  return prev + next + index
})
console.log(res)

8.ES6 新增的数组拓展。

  • Array.of() 用于将一组值转换为数组。
  • Array.copyWithin() 将指定位置的成员复制到其它位置(会覆盖原来位置的成员),然后返回数组.

-----未完待续--------