List of js array methods

1、at()

Pass an array index as an argument to a function and return the array element corresponding to that index.

const array = [1,2,3,4,5];
console.log(array.at(1)); // 2
console.log(arr.at(-1)); // 5
console.log(arr.at(-2)); // 4
console.log(arr.at(10)); // undefined 超出最大下标,那么就返回undefined,不会报错

2、concat()

Merge arrays, returning a new array.

const array = [1,2,3,4,5];
const arr = ['a','v','d'];
console.log(array.concat(arr)); // [1, 2, 3, 4, 5, 'a', 'v', 'd']
console.log(arr.concat(array)); // ['a', 'v', 'd', 1, 2, 3, 4, 5]

3、reverse()

Reverses the order of array items.

const array = [1,2,3,4,5];
console.log(array.reverse()); // [5, 4, 3, 2, 1]

4、join()

Combine the elements of the array into a string, separated by parameter delimiters, separated by commas by default.

const array = [1,2,3,4,5];
console.log(array.join()); // 1,2,3,4,5
console.log(array.join('')); // 12345
console.log(array.join('-')); // 1-2-3-4-5

// 数组中以某一字符为基准换行
console.log(array.join().replace(/[,]/g,'$&\r\n')); 
// 1,
// 2,
// 3,
// 4,
// 5

5、push()

Adds the argument to the end of the original array and returns the length of the array.

const array = [1,2,3,4,5];
console.log(array.push(9)); // 6
console.log(array); // [1, 2, 3, 4, 5, 9]

//遍历取数组中的某一项
const tableDataAdd = [
  { agentCode: '789456', agentName: '张三' },
  { agentCode: '234486', agentName: '李四'},
  { agentCode: '765455', agentName: '王五' },
];
const tableDataAddArr = [];
tableDataAdd.map((item, index) => {
  tableDataAddArr.push(item.agentCode);
})
console.log(tableDataAddArr); // ['789456', '234486', '765455']

6、pop()

Removes the last item from the end of the array and returns the removed item.

const array = [1,2,3,4,5];
console.log(array.pop()); // 5
console.log(array); // [1, 2, 3, 4]

7、unshift()

Adds the argument to the beginning of the original array and returns the length of the array.

const array = [1,2,3,4,5];
console.log(array.unshift(9)); // 6
console.log(array); // [9, 1, 2, 3, 4, 5]

8、shift()

Delete the first item in the original array and return the removed item.

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

9、slice()

Intercept, the original array remains unchanged, and a new array is returned.

const array = [1,2,3,4,5];
console.log(array.slice(1)); // [2, 3, 4, 5]
console.log(array.slice(1,3)); // [2, 3] 包括参数1,不包括参数2
console.log(array.slice(-1)); // [5]

10、splice()

Change the original array, you can delete, insert, replace operations.

const array = [1,2,3,4,5];
//删除:2个参数,(1,2)从下标1开始删除2个
console.log(array.splice(1,2)); //  [2, 3]
console.log(array); //  [1, 4, 5]

//替换:3个参数,(1,3,5)从下标1开始,长度为3的数组元素替换成5
console.log(array.splice(1,3,5)); //  [2, 3, 4]
console.log(array); // [4, 2, 3, 4, 5][1, 5, 5]

//插入:3个参数,(1,0,5)在下标为1处添加一项5(中间项为0)
console.log(array.splice(1,0,5)); // 
console.log(array); // [1, 5, 2, 3, 4, 5]

11、sort()

Defaults to sorting values ​​as strings alphabetically and ascending, altering the original array.

const array = ['D', 'B', 'A', 'C'];
console.log(array.sort()); // ['A', 'B', 'C', 'D']

//用比较函数解决sort排序错误问题
const array = [4, 1, 3, 2, 10];
console.log(array.sort()); // [1, 10, 2, 3, 4]
console.log(array.sort((a, b) => a - b)); // [1, 2, 3, 4, 10]
console.log(array.sort((a, b) => b - a)); // [10, 4, 3, 2, 1]

12、indexOf()

Find whether the parameter exists in the array, if it exists, return the subscript, if not, return -1.

const array = [1, 2, 3, 4, 5];
console.log(array.indexOf(4)); // 3
console.log(array.indexOf(7)); // -1

//判断数组内元素是否存在
const list = [
  { code: '01',name: '张山' },
  { code: '02',name: '李四' },
  { code: '03',name: '王五' },
]
const listFilter = list.filter(optionItem => {
  return ['02'].indexOf(optionItem.code) > -1;
});
console.log(listFilter);// [{code: '02', name: '李四'}]

13、includes()

Whether an array contains the given value, returns a Boolean value.

const array = [1, 2, 3, 4, 5];
console.log(array.includes(3)); // true
console.log(array.includes(7)); // false
console.log(array.includes(3,2)); // true

14、fill()

Fill array elements with static values.

const array = [1, 2, 3, 4, 5];
console.log(array.fill(4)); // [4, 4, 4, 4, 4]

Guess you like

Origin blog.csdn.net/m0_73460278/article/details/128924099