JavaScript_js对象_Array

194 阅读10分钟

1、concat()

描述

连接若干数组

语法

array1.concat(array2,array3,...,arrayX)

注意

不会改变现有数组,仅返回连接后的数组

实例

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);// expected output: Array ["a", "b", "c", "d", "e", "f"]

2、copywithin()

描述

从数组的指定位置拷贝元素到数组的另一个指定位置中

语法

array.copyWithin(target, start, end)

  • target:必需 复制到指定索引位置
  • start:可选 元素复制的起始位置
  • end:可选 停止复制的索引位置,默认为array.length,如果为负数则表示倒数

注意

实例

//复制数组前两个元素到后面两个元素上
var array1=["a","b","c","d"];
console.log(array1.copyWithin(2,0));//ecpected output: a,b,a,b
//复制数组前两个元素到第三和第四位置上
var array2=["a","b","c","d","e","f"];
console.log(array2.copyWithin(2,0,2));//ecpected output: a,b,a,b,e,f

3、entries()

描述

返回一个数组的迭代对象,该对象包含数组的键值对(key/value)

语法

array.entries()

注意

实例

const array1 = ['a', 'b', 'c'];
const iterator1 = array1.entries();
console.log(iterator1.next().value);// expected output: Array [0, "a"]
console.log(iterator1.next().value);// expected output: Array [1, "b"]

4、every()

描述

检测数组每个元素是否符合条件

语法

array.every(function(currentValue,index,arr), thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 对象作为该执行回调时使用,传递给函数,用作 "this" 的值,如果省略了 thisValue ,"this" 的值为 "undefined"

注意

  • every() 不会对空数组进行检测。
  • every() 不会改变原始数组。

实例

const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));// expected output: true

5、fill()

描述

使用固定值填充数组

语法

array.fill(value,start,end)

  • value:必需 填充的值
  • start:可选 开始填充位置
  • end:可选 停止填充位置(默认为array.length)

注意

实例

const array = ['a', 'b', 'c',"d"];
console.log(array.fill("A",2,4)) //ecpected output: a,b,A,A

6、filter()

描述

检测数组元素,并返回符合条件的所有元素的数组

语法

array.filter(function(currentValue,index,arr), thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 对象作为该执行回调时使用,传递给函数,用作 "this" 的值,如果省略了 thisValue ,"this" 的值为 "undefined"

注意

  • filter() 方法创建新的数组,存放所有符合条件的元素
  • filter() 不会对空数组进行检测
  • filter() 不会改变原始数组

实例

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);// expected output: Array ["exuberant", "destruction", "present"]

7、find()

描述

返回符合传入测试条件的数组元素

语法

array.find(function(currentValue, index, arr),thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 传递给函数的值一般用 "this"值,如果这个参数为空,"undefined"会传递给"this"值

注意

  • find()方法返回通过测试的数组的第一个元素的值
  • find()方法为数组中的每个元素都调用一次函数执行,当找到符合条件的元素时,之后的值不在调用执行函数,如果没有符合的元素则返回undefined
  • find() 对于空数组,函数是不会执行的。
  • find() 并没有改变数组的原始值。

实例

const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);// expected output: 12

8、findIndex()

描述

返回符合传入测试条件的数组元素索引

语法

array.findIndex(function(currentValue, index, arr),thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 传递给函数的值一般用 "this"值,如果这个参数为空,"undefined"会传递给"this"值

注意

  • findIndex()方法返回通过测试的数组的第一个元素的索引
  • findIndex()方法为数组中的每个元素都调用一次函数执行,当找到符合条件的元素时,之后的值不在调用执行函数,如果没有符合的元素则返回-1
  • findIndex() 对于空数组,函数是不会执行的。
  • findIndex() 并没有改变数组的原始值。

实例

const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));// expected output: 3

9、forEach()

描述

数组每个元素都执行一次回调函数

语法

array.forEach(function(currentValue, index, arr), thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 传递给函数的值一般用 "this"值,如果这个参数为空,"undefined"会传递给"this"值

注意

  • forEach() 对于空数组是不会执行回调函数的

实例

const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"

10、includes()

描述

判断数组是否包含指定值

语法

arr.includes(searchElement)

arr.includes(searchElement, fromIndex)

  • searchElement:必选,需要查找的元素值
  • fromIndex:可选,从指定索引开始查找searchElement,如果为负,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0

注意

实例

var arr = ['a', 'b', 'c'];
arr.includes('c', 3);   //ecpected output false
arr.includes('c', 100); // ecpected output false

11、indexOf()

描述

搜索数组元素并返回其索引

语法

array.indexOf(item,start)

  • item:必须,查找的元素
  • start:可选,元素开始检索的位置,取值范围为0到length-1,默认从0开始检索

注意

实例

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
console.log(a)//expected output 2

12、isArray()

描述

判断是否为数组

语法

Array.isArray(obj)

注意

实例

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = Array.isArray(fruits);
console.log(a)//expected output true

13、join()

描述

把数组拼接成一个字符串

语法

array.join(separator)

  • separator:可选,指定的连接符

注意

实例

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(" and ");
console.log(energy)//expected output: Banana and Orange and Apple and Mango

14、keys()

描述

返回数组的可迭代对象,包含原始数组的键(key)

语法

array.keys()

注意

实例

const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

for (const key of iterator) {
  console.log(key);
}

// expected output: 0
// expected output: 1
// expected output: 2

15、lastIndexOf()

描述

搜索数组中的元素,并返回它最后出现的位置

语法

array.lastIndexOf(item,start)

  • item:必需,待检索的字符串
  • start:可选,从指定索引开始查找item,取值范围为0到length-1,默认从最后一个字符处开始检索

注意

实例

var fruits=["Banana","Orange","Apple","Mango","Banana","Orange","Apple"];
var a = fruits.lastIndexOf("Apple");
console.log(a)//ecpected output:6

16、map()

描述

通过指定函数处理数组的每个元素,并返回处理后的数组

语法

array.map(function(currentValue,index,arr), thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 传递给函数的值一般用 "this"值,如果这个参数为空,"undefined"会传递给"this"值

注意

  • map() 不会对空数组进行检测。
  • map() 不会改变原始数组。

实例

const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);// expected output: Array [2, 8, 18, 32]

17、pop()

描述

删除数组的最后一个元素并返回删除的元素

语法

array.pop()

注意

此方法改变数组的长度!

实例

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());// expected output: "tomato"
console.log(plants);// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants);// expected output: Array ["broccoli", "cauliflower", "cabbage"]

18、push()

描述

向数组的末尾添加一个或更多元素,并返回新的长度

语法

array.push(item1, item2, ..., itemX)

注意

  • 新元素将添加在数组的末尾
  • 此方法改变数组的长度

实例

const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);// expected output: 4
console.log(animals);// expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens', 'cats', 'dogs');
console.log(animals);// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

19、reduce()

描述

将数组元素计算为一个值(从左到右)

语法

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

  • function(total, currentValue, currentIndex, arr):必需,用于执行每个数组元素的函数
  • total:必需,初始值,或者计算结束后的返回值
  • currentValue:必需,当前元素
  • currentIndex:可选,当前元素的索引
  • arr:可选,当前元素所属的数组对象
  • initialValue:可选,传递给函数的初始值

注意

reduce() 对于空数组是不会执行回调函数的

实例

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));// expected output: 15

20、reduceRight()

描述

将数组元素计算为一个值(从右到左

语法

array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)

  • function(total, currentValue, currentIndex, arr):必需,用于执行每个数组元素的函数
  • total:必需,初始值,或者计算结束后的返回值
  • currentValue:必需,当前元素
  • currentIndex:可选,当前元素的索引
  • arr:可选,当前元素所属的数组对象
  • initialValue:可选,传递给函数的初始值

注意

reduceRight() 对于空数组是不会执行回调函数的

实例

const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
  (accumulator, currentValue) => accumulator.concat(currentValue)
);
console.log(array1);// expected output: Array [4, 5, 2, 3, 0, 1]

21、reverse()

描述

反转数组的元素顺序

语法

array.reverse()

注意

改变原数组

实例

const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);// expected output: "array1:" Array ["one", "two", "three"]
const reversed = array1.reverse();
console.log('reversed:', reversed);// expected output: "reversed:" Array ["three", "two", "one"]
// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);// expected output: "array1:" Array ["three", "two", "one"]

22、shift()

描述

删除并返回数组的第一个元素

语法

array.shift()

注意

此方法改变数组的长度!

实例

const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);// expected output: Array [2, 3]
console.log(firstElement);// expected output: 1

23、slice()

描述

选取数组的的一部分,并返回一个新数组

语法

array.slice(start, end)

  • start:可选,指定从何处开始选取,如果是负数,则从尾部开始算起始位置
  • end:可选,指定从何处开始结束,如果是负数,则从尾部开始算结束位置,默认为到数组结束

注意

slice() 方法不会改变原始数组

实例

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));// expected output: Array ["bison", "camel", "duck", "elephant"]

24、some()

描述

检测数组元素中是否有元素符合指定条件

语法

array.some(function(currentValue,index,arr),thisValue)

  • function(currentValue,index,arr): 必需 数组中每个元素都会执行这个函数
  • currentValue:必需 当前元素的值
  • index:可选 当前元素的索引值
  • arr:可选 当前元素属于的对象数组
  • thisValue:可选 传递给函数的值一般用 "this"值,如果这个参数为空,"undefined"会传递给"this"值

注意

  • some() 不会对空数组进行检测
  • some() 不会改变原始数组

实例

const array = [1, 2, 3, 4, 5];// checks whether an element is even
const even = (element) => element % 2 === 0;
console.log(array.some(even));// expected output: true

25、sort()

描述

对数组的元素进行排序

语法

array.sort(sortfunction)

  • sortfunction:可选。规定排序顺序。必须是函数

注意

这种方法会改变原始数组

实例

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);// expected output: Array ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);// expected output: Array [1, 100000, 21, 30, 4]

26、splice()

描述

从数组中添加或删除元素

语法

array.splice(index,howmany,item1,.....,itemX)

  • index:必需,规定从何处添加/删除
  • howmany:可选,规定删除的个数,必须为数组,可以为0,如果未规定此参数默认从index开始删到结尾
  • item1,...itemX:可选,需要添加到数组的新元素

注意

这种方法会改变原始数组

实例

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);// expected output: Array ["Jan", "Feb", "March", "April", "May"]

27、toString()

描述

把数组转换为字符串,并返回结果

语法

array.toString()

注意

数组中的元素之间用逗号分隔

实例

const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());// expected output: "1,2,a,1a"

28、unshift()

描述

向数组的开头添加一个或更多元素,并返回新的长度

语法

array.unshift(item1,item2, ..., itemX)

  • item1,...itemX:可选,向数组起始位置添加一个或多个元素

注意

该方法将改变数组的数目

实例

const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));// expected output: 5
console.log(array1);// expected output: Array [4, 5, 1, 2, 3]