- 使用filter()方法:
js
复制代码
const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.filter(item => item !== elementToRemove);
- 使用splice()方法:
js
复制代码
const array = [1, 2, 3, 4, 5];
const indexToRemove = array.indexOf(3);
array.splice(indexToRemove, 1);
- 使用forEach()方法和slice()方法:
js
复制代码
const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
let newArray = [];
array.forEach(item => {
if (item !== elementToRemove) {
newArray.push(item);
}
});
- 使用reduce()方法:
js
复制代码
const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.reduce((acc, item) => {
if (item !== elementToRemove) {
acc.push(item);
}
return acc;
}, []);
- 使用for循环和splice()方法:
js
复制代码
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--; // 解决删除元素后下标的重新计算问题
}
}
- 使用filter()方法和includes()方法:
js
复制代码
const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.filter(item => !elementToRemove.includes(item));
- 使用for循环和slice()方法:
js
复制代码
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]);
}
}
- 使用for循环和push()方法:
js
复制代码
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]);
}
}
- 使用filter()方法和indexOf()方法:
js
复制代码
const array = [1, 2, 3, 4, 5];
const elementToRemove = 3;
const newArray = array.filter(item => array.indexOf(item) !== array.indexOf(elementToRemove));
- 使用for循环和concat()方法:
js
复制代码
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]);
}
}
- 使用for循环和splice()方法:
js
复制代码
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);
}
}
- 使用for循环和pop()方法:
js
复制代码
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--;
}
}
- 使用for循环和shift()方法:
js
复制代码
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--;
}
}
- 使用for循环和unshift()方法:
js
复制代码
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--;
}
}
- 使用for循环和delete关键字:
js
复制代码
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];
}
}