JS数组常用操作方法总结

1、push()

push()方法可以向数组的末尾添加一个或者多个元素,并返回新的长度.

const arr = [1, 2, 3, 4, 5, 6]
arr.push(7) // [1, 2, 3, 4, 5, 6, 7]
2、pop()

pop() 方法用于删除并返回数组的最后一个元素。

const arr = [1, 2, 3, 4, 5, 6]
console.log(arr.pop())  // 6
console.log(arr)    // [1, 2, 3, 4, 5]
3、unshift()

unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。

const arr = [1, 2, 3, 4, 5, 6]
console.log(arr)    // [0, 1, 2, 3, 4, 5, 6]
4、shift()

shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。

    const arr = [1, 2, 3, 4, 5, 6]
    console.log(arr.shift())    // 1
    console.log(arr)    // [3, 4, 5, 6]
5、isArray()

isArray() 这个方法用来判断一个对象是不是数组,是的话返回true,否则返回false

    const arr = [1, 2, 3, 4, 5, 6]
    const str = 'abc'
    console.log(Array.isArray(arr)) // true
    console.log(Array.isArray(str)) // false
6、map()

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]
7、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"]
8、every()

every() 方法测试一个数组内的所有元素是否都能通过指定函数的测试。它返回一个布尔值。

    const arr = [1, 2, 3, 4, 5, 6]
    const newArray = arr.every(item => item > 0)
    const newArray1 = arr.every(item => item > 3)
    console.log(newArray)       // true
    console.log(newArray1)      // false
9、some()

some() 方法测试数组中是否至少有一个元素通过了由提供的函数实现的测试。如果在数组中找到一个元素使得提供的函数返回 true,则返回 true;否则返回 false。它不会修改数组。

    const arr = [1, 2, 3, 4, 5, 6]
    const newArray = arr.some(item => item > 7)
    const newArray1 = arr.some(item => item > 3)
    console.log(newArray)	// false  数组中没有大于7的元素
    console.log(newArray1)	// true	数组中4,5,6大于3
10、splice()

splice() 方法通过移除或者替换已存在的元素和/或添加新元素就地改变一个数组的内容。

    const arr = [1, 2, 3, 4, 5, 6]
    //在索引6位置加一个元素7
    arr.splice(6,0,7)  
    console.log(arr)    // [1, 2, 3, 4, 5, 6, 7]
    //在索引为0的位置将元素替换为0
    arr.splice(0,1,0)
    console.log(arr)    // [0, 2, 3, 4, 5, 6, 7]
    //从索引0开始删除3个元素
    arr.splice(0,3)
    console.log(arr)    // [4, 5, 6, 7]
11、slice()

语法:slice(start,end)

slice() 方法返回一个新的数组对象,这一对象是一个由 start 和 end 决定的原数组的浅拷贝(包括 start,不包括 end),其中 start 和 end 代表了数组元素的索引。原始数组不会被改变

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"]

console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]

console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
12、indexOf()

indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。

    const arr = [1, 2, 3, 4, 5]
    const res = arr.indexOf(5)
    const res1 = arr.indexOf(8)
    console.log(res)	// 4
    console.log(res1)	// -1
13、includes()

includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。

    const arr = [1, 2, 3, 4, 5]
    const res = arr.includes(5)
    const res1 = arr.includes(8)
    console.log(res)	// true
    console.log(res1)	// false
14、concat()

concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

   
    const arr1 = [1, 2, 3]
    const arr2 = [4, 5, 6]
    const arr3 = [7, 8, 9]
    console.log(arr1.concat(arr2))	// [1, 2, 3, 4, 5, 6]
    console.log(arr1.concat(arr2, arr3))	// [1, 2, 3, 4, 5, 6, 7, 8, 9]
15、join()

join() 方法将一个数组的所有元素连接成一个字符串并返回这个字符串,用逗号或指定的分隔符字符串分隔。如果数组只有一个元素,那么将返回该元素而不使用分隔符。

    const arr = [1, 2, 3]
    console.log(arr.join())	// 1,2,3
    console.log(arr.join(''))	// 123
16、split()

split() 方法用于把一个字符串分割成字符串数组。

    const str = "hello world"
    console.log(str.split(''))	// ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
17、forEach()

forEach() 方法对数组的每个元素执行一次给定的函数。

const array1 = ['a', 'b', 'c'];

array1.forEach((element) => console.log(element));

// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
18、sort()

sort() 方法用于对数组的元素进行排序。sort()默认排序10以内的,如过需要排序的数组有大于10,则需要一个function函数配合

    const array = [1,9,3,6,5,7,2,0]
    console.log(array.sort())	// [0, 1, 2, 3, 5, 6, 7, 9]

	const array1 = [1, 30, 4, 21, 100000];
	//顺序
    console.log(array1.sort(function (a, b) {
      return a - b
    }))	// [1, 4, 21, 30, 100000]
	//逆序
    console.log(array1.sort(function (a, b) {
      return b - a
    }))	// [100000, 30, 21, 4, 1]
19、reverse()

reverse() 方法用于颠倒数组中元素的顺序。

    const arr = [1, 2, 3, 4, 5]
    console.log(arr.reverse()) // [5, 4, 3, 2, 1]
20、find()

find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined

    const arr = [1, 2, 3, 4, 5]
    console.log(arr.find(item => {
      return item > 4
    }))	// 5
    console.log(arr.find(item => {
      return item > 5
    }))	// undefined
21、findIndex()

findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。

    const arr = [1, 2, 3, 4, 5]
    console.log(arr.findIndex(item => {
      return item > 1
    }))	// 1 
    console.log(arr.findIndex(item => {
      return item > 5
    }))	// -1
22、fill()

fill() 方法用一个固定值填充一个数组中从起始索引(默认为 0)到终止索引(默认为 array.length)内的全部元素。它返回修改后的数组。

const array1 = [1, 2, 3, 4];

// Fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// Expected output: Array [1, 2, 0, 0]

// Fill with 5 from position 1
console.log(array1.fill(5, 1));
// Expected output: Array [1, 5, 5, 5]

console.log(array1.fill(6));
// Expected output: Array [6, 6, 6, 6]
23、keys()、values()、entries()

keys()是对键名的遍历、values()对键值的遍历、entries()是对键值对的遍历。

//key()
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



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

for (const value of iterator) {
  console.log(value);
}
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"



//entries()
const array = ['a', 'b', 'c'];
const iterator = array.entries();
for (const key of iterator) {
  console.log(key);
}

// Expected output: [0, "a"]
// Expected output: [1, "b"]
// Expected output: [2, "c"]
25、补充删除素质元素方法delete

delete可以删除数组元素,并且不会改变数组长度

const arr = [1, 2, 3, 4, 5]
delete arr[0]
console.log(arr)	//  [空白, 2, 3, 4, 5]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值