从生活入手学编程(2):js删除指定元素的15种方法

发布时间:2024年01月10日

1. 使用filter()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.filter(item => item !== elementToRemove);

2. 使用splice()方法:

const array = [1, 2, 3, 4, 5];
const indexToRemove = array.indexOf(3);
array.splice(indexToRemove, 1);

3. 使用forEach()方法和slice()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
let newArray = [];
array.forEach(item => {
  if (item !== elementToRemove) {
    newArray.push(item);
  }
});

4. 使用reduce()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.reduce((acc, item) => {
  if (item !== elementToRemove) {
    acc.push(item);
  }
  return acc;
}, []);

5. 使用for循环和splice()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
for (let i = 0; i < array.length; i++) {
  if (array[i] === elementToRemove) {
    array.splice(i, 1);
    i--; // 解决删除元素后下标的重新计算问题
  }
}

6. 使用filter()方法和includes()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.filter(item => !elementToRemove.includes(item));

7. 使用for循环和slice()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = [];
for (let i = 0; i < array.length; i++) {
  if (array[i] !== elementToRemove) {
    newArray.push(array[i]);
  }
}

8. 使用for循环和push()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = [];
for (let i = 0; i < array.length; i++) {
  if (array[i] !== elementToRemove) {
    newArray.push(array[i]);
  }
}

9. 使用filter()方法和indexOf()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.filter(item => array.indexOf(item) !== array.indexOf(elementToRemove));

10. 使用for循环和concat()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
let newArray = [];
for (let i = 0; i < array.length; i++) {
  if (array[i] !== elementToRemove) {
    newArray = newArray.concat(array[i]);
  }
}

11. 使用for循环和splice()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
for (let i = array.length - 1; i >= 0; i--) {
  if (array[i] === elementToRemove) {
    array.splice(i, 1);
  }
}

12. 使用for循环和pop()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
for (let i = 0; i < array.length; i++) {
  if (array[i] === elementToRemove) {
    array[i] = array[array.length - 1];
    array.pop();
    i--;
  }
}

13. 使用for循环和shift()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
for (let i = 0; i < array.length; i++) {
  if (array[i] === elementToRemove) {
    for (let j = i; j < array.length - 1; j++) {
      array[j] = array[j + 1];
    }
    array.pop();
    i--;
  }
}

14. 使用for循环和unshift()方法:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
for (let i = 0; i < array.length; i++) {
  if (array[i] === elementToRemove) {
    for (let j = i; j < array.length - 1; j++) {
      array[j] = array[j + 1];
    }
    array.length--;
    i--;
  }
}

15. 使用for循环和delete关键字:

const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
for (let i = 0; i < array.length; i++) {
  if (array[i] === elementToRemove) {
    delete array[i];
  }
}

文章来源:https://blog.csdn.net/joe_g12345/article/details/135511231
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。