简体   繁体   English

从 object 数组中删除元素的最快方法是什么

[英]what is fastest way to remove elements from array of object

I am removing an object from an array and came to know different methods mention below:我正在从数组中删除 object 并了解以下提到的不同方法:

let this is my array:让这是我的数组:

var user = [{id:1}, {id:2}, {id:3}, {id:4}];

First method using splice:使用拼接的第一种方法:

let index = user.findIndex(e=>{
    return e.id == 3;
});
array.splice(index, 1);

Second method using filter:使用过滤器的第二种方法:

array = array.filter((data)=>{
   return data.id !== 3;
});

Third method using third party library(like lodash):使用第三方库的第三种方法(如 lodash):

_.remove(array, function(data) {
    return data.id == 3 ;
});

which is the fastest among these or is there any more efficient way in terms of performance to remove specific element(which is an object).这是其中最快的,或者在删除特定元素(这是一个对象)的性能方面是否有更有效的方法。

If you want to know the execution time of each of options mentioned, you can use -如果您想知道提到的每个选项的执行时间,您可以使用 -

console.time("Time");
// place your options one by one in between
console.timeEnd("Time");

This will really help with analyzing performance of each piece of code.这将真正有助于分析每段代码的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM