最快的方法从数组中删除某些值

时间:2017-10-09 12:27:02

标签: javascript arrays

我有一个看起来像这样的数组:

const data =  [
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '04b073133c7843248a7a3dbc968f75a0', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '4420cc109ec54214b68edc906b18e44a', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.75, 0.67, 5.58, 0.5625, 0.129375, 0.691875, 4.185, 0.96255, 5.14755, 0.5025, 0.115575, 0.618075],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '90a7cbf1cf4e4043889626c4119d4b4d', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '0f04f1ff385541d3a8d9ea2f0d85482b', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '0f04f1ff385541d3a8d9ea2f0d85482b', 'network1', 'affiliate',
        1232113, 1232133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];

我想要实现(但不知道如何)是删除所有条目(以最快的方式),看起来像这个数组中的最后一个,例如[Wed Sep 20 09:00:00 GMT+02:00 2017, SKYSCANNER, 0f04f1ff385541d3a8d9ea2f0d85482b, network1, affiliate, 1232113, 1232133, , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

基本上,如果第8个位置之后的所有值都为零,则不需要输入,必须将其删除。

通常在我的情况下,这样的数组可以有5-15k个条目,所以我想知道实现这个目的的最快方法是什么?有人可以提供一个工作片段吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

以下我的机器需要11毫秒。正如Cerbrus所写,只需使用过滤器,15k并不多:

const data = [
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '04b073133c7843248a7a3dbc968f75a0', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '4420cc109ec54214b68edc906b18e44a', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.75, 0.67, 5.58, 0.5625, 0.129375, 0.691875, 4.185, 0.96255, 5.14755, 0.5025, 0.115575, 0.618075],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '90a7cbf1cf4e4043889626c4119d4b4d', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '0f04f1ff385541d3a8d9ea2f0d85482b', 'network1', 'affiliate',
        1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575],
    ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '0f04f1ff385541d3a8d9ea2f0d85482b', 'network1', 'affiliate',
        1232113, 1232133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];

let test = [];
for (let i = 0; i < 3000; i += 1) {
    test = test.concat(data);
}

const start = performance.now();

const result = test.filter(
               // reject entry, if not 
                      // every value from 8th to last
                                              // is 0
    (entry) => !entry.slice(8).every((val) => 0 === val)
);

console.log(performance.now() - start);

答案 1 :(得分:2)

如果您想要快速代码,只需检查单独的值。

或(||)会短路,因此来自e[7] - e[18]的第一个具有值的条目将使该行通过测试,这意味着{{1将继续前进到下一行。

filter

(时间码借自Yoshi的回答)

(对我来说)这比Yoshi使用的const data = [ ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '04b073133c7843248a7a3dbc968f75a0', 'network1', 'affiliate', 1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575], ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '4420cc109ec54214b68edc906b18e44a', 'network1', 'affiliate', 1141338.0, 18164.0, 0.75, 0.67, 5.58, 0.5625, 0.129375, 0.691875, 4.185, 0.96255, 5.14755, 0.5025, 0.115575, 0.618075], ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '90a7cbf1cf4e4043889626c4119d4b4d', 'network1', 'affiliate', 1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575], ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '0f04f1ff385541d3a8d9ea2f0d85482b', 'network1', 'affiliate', 1141338.0, 18164.0, 0.08, 0.07, 0.62, 0.06, 0.0138, 0.0738, 0.465, 0.10695, 0.57195, 0.0525, 0.012075, 0.064575], ['Wed Sep 20 09:00:00 GMT+02:00 2017', 'SKYSCANNER', '0f04f1ff385541d3a8d9ea2f0d85482b', 'network1', 'affiliate', 1232113, 1232133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; let test = []; for (let i = 0; i < 3000; i += 1) { test = test.concat(data); } const start = performance.now(); const result = test.filter(e => e[7]||e[8]||e[9]||e[10]||e[11]||e[12]||e[13]||e[14]||e[15]||e[16]||e[17]||e[18]); console.log(performance.now() - start); / slice组合快两倍,但是可读/可维护的一半。

取决于您是否更喜欢可读代码,或者您是否希望将所有指南抛出窗口以获取原始速度,这取决于您。