当前位置: 首页 > news >正文

JS :移除数组中的指定数据

1. Array.prototype.filter() 方法

filter() 方法会创建一个新数组,包含所有通过测试的元素。可以通过过滤掉不需要的元素来实现移除。

const array = [1, 2, 3, 4, 5];
const itemToRemove = 3;

const newArray = array.filter(item => item !== itemToRemove);
console.log(newArray); // 输出: [1, 2, 4, 5]

优点:

  • 不会修改原数组,返回一个新数组。

  • 适合移除多个匹配项。

缺点:

  • 如果需要修改原数组,需要重新赋值。


2. Array.prototype.splice() 方法

splice() 方法可以直接修改原数组,删除指定索引的元素。

const array = [1, 2, 3, 4, 5];
const itemToRemove = 3;

const index = array.indexOf(itemToRemove);
if (index !== -1) {
    array.splice(index, 1);
}
console.log(array); // 输出: [1, 2, 4, 5]

优点:

  • 直接修改原数组。

  • 适合移除单个匹配项。

缺点:

  • 只能移除第一个匹配项,如果需要移除所有匹配项需要循环。


3. Array.prototype.indexOf()Array.prototype.slice()

结合 indexOf() 和 slice(),可以创建一个新数组,排除指定元素。

const array = [1, 2, 3, 4, 5];
const itemToRemove = 3;

const index = array.indexOf(itemToRemove);
if (index !== -1) {
    const newArray = [...array.slice(0, index), ...array.slice(index + 1)];
    console.log(newArray); // 输出: [1, 2, 4, 5]
}

优点:

  • 不会修改原数组。

  • 适合移除单个匹配项。

缺点:

  • 代码略显复杂。


4.  Array.prototype.reduce() 方法

reduce() 方法可以通过遍历数组,创建一个新数组,排除指定元素。

const array = [1, 2, 3, 4, 5];
const itemToRemove = 3;

const newArray = array.reduce((acc, item) => {
    if (item !== itemToRemove) {
        acc.push(item);
    }
    return acc;
}, []);
console.log(newArray); // 输出: [1, 2, 4, 5]

优点:

  • 灵活,适合复杂的移除逻辑。

  • 不会修改原数组。

缺点:

  • 代码略显复杂。


5. 使用 Set 结构

如果需要移除多个重复项,可以将数组转换为 Set,然后再转换回数组。

const array = [1, 2, 3, 4, 5, 3];
const itemToRemove = 3;

const newArray = Array.from(new Set(array.filter(item => item !== itemToRemove)));
console.log(newArray); // 输出: [1, 2, 4, 5]

优点:

  • 可以移除所有匹配项。

  • 适合去重场景。

缺点:

  • 需要额外转换为 Set


6. Array.prototype.forEach() 和 Array.prototype.push() 

通过遍历数组,将不需要移除的元素添加到新数组中。

const array = [1, 2, 3, 4, 5];
const itemToRemove = 3;

const newArray = [];
array.forEach(item => {
    if (item !== itemToRemove) {
        newArray.push(item);
    }
});
console.log(newArray); // 输出: [1, 2, 4, 5]

优点:

  • 不会修改原数组。

  • 适合移除多个匹配项。

缺点:

  • 代码略显冗长。


总结

方法是否修改原数组适合场景
filter()移除多个匹配项,返回新数组
splice()移除单个匹配项,直接修改原数组
indexOf() + slice()移除单个匹配项,返回新数组
reduce()复杂移除逻辑,返回新数组
Set去重并移除多个匹配项
forEach() + push()移除多个匹配项,返回新数组

根据你的具体需求,选择合适的方法可以提高代码的效率和可读性。希望本文能帮助你更好地掌握 JavaScript 中数组的操作!

相关文章:

  • LeetCode 热题 100 53. 最大子数组和
  • 老牌工具,16年依然抗打!
  • 计算机毕业设计SpringBoot+Vue.js林业产品推荐系统 农产品推荐系统 (源码+文档+PPT+讲解)
  • Github 2025-02-28 Java开源项目日报 Top9
  • Spring Boot spring-boot-maven-plugin 参数配置详解
  • 使用python解决硬币找零问题
  • jvm内存模型,类加载机制,GC算法,垃圾回收器,jvm线上调优等常见的面试题及答案
  • python文件操作
  • 医脉云枢:中医药典籍知识图谱与非遗传承多维可视化系统
  • 英文分词方法对比:NLTK等五种分词工具的性能分析与适用场景
  • 浅浅初识AI、AI大模型、AGI
  • 汽车无人驾驶系统中的防撞设计
  • vue3 父组件调用子组件的方法/父组件获取子组件的值
  • 内容中台是什么?内容管理平台解析
  • 选择排序法
  • 【Qt QML】QML鼠标事件(MouseArea)
  • C#并发集合-ConcurrentQueue
  • 2024年第十五届蓝桥杯大赛软件赛省赛Python大学A组真题解析《更新中》
  • kkfileview部署
  • 0x04 jdbc和mybatis
  • 前四个月人民币贷款增加10.06万亿元,4月末M2余额同比增长8%
  • 金正恩观摩朝鲜人民军各兵种战术综合训练
  • 科普|揭秘女性压力性尿失禁的真相
  • 男子发寻母视频被警方批评教育,律师:发寻亲信息是正当行为
  • 中共中央、国务院印发《生态环境保护督察工作条例》
  • 消费维权周报|上周涉手机投诉较多,涉拍照模糊、屏幕漏液等