使用过滤器和拼接删除数组中的对象哪一种是 JavaScript 中的最佳方法? [英] Remove object in array using filter and splice which one is best approach in JavaScript?

查看:18
本文介绍了使用过滤器和拼接删除数组中的对象哪一种是 JavaScript 中的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两种方法删除数组中的对象:-拼接和过滤.

Hi I delete an object in an array using two approaches:- splice and filter.

在这里拼接代码:-

(this.myArray).splice((this.myArray).indexOf(myobject), 1);

此处过滤代码:-

(this.myArray).filter(obj => obj !== myobject);

请告诉我们两者之间的区别,哪个是最好的方法?

Please tell us differences between both and which one is the best approach?

推荐答案

我认为这里的主要区别在于:

I think that the main difference here is:

  • splice - 允许您从此特定数组中删除元素
  • filter - 不会触及输入数组,而是会创建并返回新的过滤数组
  • splice - lets you remove an element from this particular array
  • filter - won't touch the input array and will create and return new filttered array

angular 与这里无关,说到速度,slice 会胜出

angular has nothing to do here and when it comes to speed, splice will win

和小测试作为证明 https://jsperf.com/array-splice-vs-array-filter/1

这篇关于使用过滤器和拼接删除数组中的对象哪一种是 JavaScript 中的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆