简体   繁体   English

javascript-Lodash中的javascript对象哈希与lodash foreach删除

[英]javascript object hash vs lodash foreach in lodash remove

I'm trying to delete some data from a complex object. 我正在尝试从复杂对象中删除一些数据。

The format of the original data is as follows. 原始数据的格式如下。

let originData = 
[
    {
       name : exampleDepth1,
       depth1Data : 
       [
            {
                 name : exampleDepth2,
                 depth2Data : 
                 [
                     {
                         code: 1234///will be delete from that data
                     },
                     ...
                 ]
            },
            ...
        ]
    },
    ....
]

let willbeDeletecode = [ 3, 100, 1234, 1000];

The name of the array to be deleted is the code value in the depth2Data array of originData , The name of the array to delete is willbeDeletecode . 要删除的阵列的名称是code中值depth2Data的阵列originData ,阵列删除的名称是willbeDeletecode

I'm sorry if you were uncomfortable. 对不起,如果您不舒服。

I'm trying to erase it in two ways. 我正在尝试以两种方式擦除它。

let deleteBook = {}
_.forEach(willbeDeletecode, (deleteCode) => {
  deleteBook[`${deleteCode}`] = deleteCode;
})

_.remove(originData, (depth1) => {
  _.remove(depth1.depth1Data, (depth2) => {
    /*
    // delete with object hash
    _.remove(depth2.depth2Data, (eachDepth2Data) => {
      return deleteBook[eachDepth2Data.code] === undefined
    })
    */

    /*
    // delete with forEach
    let ret = false;
     _.remove(depth2.depth2Data, (eachDepth2Data) => {
       _.forEach(willbeDeletecode, (deleteCode) => {
         if(deleteCode === eachDepth2Data.code){
             ret = true;
             return false;
            }
        })
        return ret
    })
    */
    return depth2.depth2Data.length === 0;
  })
  return depth1.depth1Data.length === 0;
})

I have two separate ways of annotating each one. 我有两种单独的注释方式。

The first is to create an object( deleteBook ) and insert the data of willbeDeletecode and use it in remove of lodash. 第一种是创建一个对象( deleteBook )并插入willbeDeletecode的数据,并将其用于除去lodash中。

The second method is entirely a comparison of all through the forEach function. 第二种方法完全是通过forEach函数进行的所有比较。

The above method was repeated 1000 times to benchmark. 重复上述方法1000次以进行基准测试。 As a result, the first method is 100 ~ 200ms and the second method is 500 ~ 700ms. 结果,第一种方法为100〜200ms,第二种方法为500〜700ms。

Of course, the willbeDeletecode is around 10 or so, but I thought Object hash was faster. 当然, willbeDeletecode大约在10左右,但是我认为对象哈希更快。 But the result was the opposite. 但是结果却相反。

If there are more variables in willbeDeletecode , will there be another conclusion? 如果willbeDeletecode有更多变量,还会有另一个结论吗? I want know why this results. 我想知道为什么会这样。

The object hash is to be preferred. 对象哈希是首选。 You could also use an ES6 Set for such purpose. 您也可以将ES6 Set用于此目的。

Such a hash solution should be faster. 这样的哈希解决方案应该更快。

One reason that you did not see this in your case, is that the first variant of your code removes the opposite of what it should. 您没有看到这种情况的一个原因是,代码的第一个变体消除了应有的反面 The _remove callback should return a truthy value when the corresponding item should be removed, yet your code returns true when the value is not in the codes that should be deleted. _remove回调应在删除相应项时返回true值,但是当该值不在应删除的代码中时,代码将返回true You should use a !== comparison: 您应该使用!==比较:

_.remove(depth2.depth2Data, (eachDepth2Data) => {
    return deleteBook[eachDepth2Data.code] !== undefined
})

As you had a === there, you probably had a lot more removals going on, giving a longer time of execution. 当您在此处===时,可能要进行更多的清除操作,因此执行时间更长。

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

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