JavaScript basic operation array method

1. Access elements:

Access elements by index: Use square brackets and an index to get the element at a specified position in the array.

const array = [1, 2, 3];
console.log(array[0]); // 输出: 1

Get the length of the array: Use lengththe property to get the length of the array.

const array = [1, 2, 3];
console.log(array.length); // 输出: 3

2. Add elements:

Add elements to the end of the array: use push()the method to add elements to the end of the array (will change the original array).

const array = [1, 2, 3];
array.push(4);
console.log(array); // 输出: [1, 2, 3, 4]

Add elements to the beginning of the array: use unshift()the method to add elements to the beginning of the array (will change the original array).

const array = [1, 2, 3];
array.unshift(0);
console.log(array); // 输出: [0, 1, 2, 3]

Insert an element at a specified position: use splice()the method to insert an element at a specified index (the original array will be changed).

const array = [1, 2, 3];
array.splice(1, 0, 1.5);
console.log(array); // 输出: [1, 1.5, 2, 3]

3. Delete elements:

Delete the element at the end of the array: Use pop()the method to delete the element at the end of the array and return the deleted element (the original array will be changed).

const array = [1, 2, 3];
const removedElement = array.pop();
console.log(array); // 输出: [1, 2]
console.log(removedElement); // 输出: 3

Delete the element at the beginning of the array: Use shift()the method to delete the element at the beginning of the array and return the deleted element (the original array will be changed).

const array = [1, 2, 3];
const removedElement = array.shift();
console.log(array); // 输出: [2, 3]
console.log(removedElement); // 输出: 1

Delete the element at the specified position: Use splice()the method to delete the element at the specified index (the original array will be changed).

const array = [1, 2, 3];
array.splice(1, 1);
console.log(array); // 输出: [1, 3]

4. Modify elements:

Modify the element at the specified position: modify the element at the specified position through index and assignment operations (the original array will be changed).

const array = [1, 2, 3];
array[1] = 5;
console.log(array); // 输出: [1, 5, 3]

5. Array traversal:

Use to forloop through the array (the original array is not changed):

const array = [1, 2, 3];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

Use forEach()the method to traverse the array (the original array will not be changed):

const array = [1, 2, 3];
array.forEach((item, index, array)=> {
  console.log(item); // 输出当前元素
  console.log(index); // 输出当前索引
  console.log(array); // 输出原始数组
});

6. Find elements:

Use find()the method to find the first element that satisfies the condition (the original array will not be changed):

const array = [1, 2, 3, 4, 5];
const foundElement = array.find((item) => {
  return item > 3; // 查找大于3的元素
});
console.log(foundElement); // 输出: 4

Use findIndex()the method to find the index of the first element that satisfies the condition (the original array will not be changed):

const array = [1, 2, 3, 4, 5];
const foundIndex = array.findIndex((item) => {
  return item > 3; // 查找大于3的元素的索引
});
console.log(foundIndex); // 输出: 3

7. Array filtering:

Use filter()the method to filter the array to find all eligible elements (the original array will not be changed):

const array = [1, 2, 3, 4, 5];
const filteredArray = array.filter((item) => {
  return item % 2 === 0; // 过滤偶数
});
console.log(filteredArray); // 输出: [2, 4]

8. Array mapping:

Use map()the method to map an array (the original array is not changed):

const array = [1, 2, 3];
const mappedArray = array.map((item) => {
  return item * 2; // 每个元素乘以2
});
console.log(mappedArray); // 输出: [2, 4, 6]

9. Array sorting:

Use sort()the method to sort the array (will change the original array):

const ascendArray = [3, 1, 2];
const descendArray = [3, 1, 2];
// 升序排序
ascendArray.sort((a, b) => a - b);
console.log(ascendArray); // 输出: [1, 2, 3]
// 降序排序
descendArray.sort((a, b) => b - a);
console.log(descendArray); // 输出: [3, 2, 1]

10. Array connection:

Use concat()the method to concatenate multiple arrays into a new array (the original array is not changed):

const array1 = [1, 2];
const array2 = [3, 4];
const newArray = array1.concat(array2);
console.log(newArray); // 输出: [1, 2, 3, 4]

11. Array slicing:

Use slice()the method to get the specified range of elements from the original array (the original array will not be changed):

const array = [1, 2, 3, 4, 5];
const slicedArray = array.slice(1, 3);
console.log(slicedArray); // 输出: [2, 3]

12. Find element index:

Use indexOf()the method to find the index of an element in an array (the original array is not changed):

const array = [1, 2, 3, 2];
console.log(array.indexOf(2)); // 输出: 1
console.log(array.lastIndexOf(2)); // 输出: 3

13. Array inversion:

Use reverse()the method to reverse the order of the array elements (the original array will be changed):

const array = [1, 2, 3];
array.reverse();
console.log(array); // 输出: [3, 2, 1]

14. Array check:

Use some()the method to check whether an element that satisfies the condition exists in the array:

const array = [1, 2, 3, 4, 5];
const hasEvenNumber = array.some((item) => {
  return item % 2 === 0; // 检查是否存在偶数
});
console.log(hasEvenNumber); // 输出: true

Use every()the method to check whether all elements in an array satisfy a condition:

const array = [1, 2, 3, 4, 5];
const allGreaterThanZero = array.every((item) => {
  return item > 0; // 检查是否所有元素都大于0
});
console.log(allGreaterThanZero); // 输出: true

15. Array cumulative operation:

Use reduce()the method to perform cumulative operations on arrays:

const array = [1, 2, 3, 4, 5];
const sum = array.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0);
console.log(sum); // 输出: 15

Guess you like

Origin blog.csdn.net/weixin_44523517/article/details/131809253