使用lodash从嵌套数组表单start和from中删除元素

时间:2017-06-15 20:08:05

标签: javascript arrays lodash

我有一个嵌套数组,例如

var arr = [
    [0,1,2,3,4],
    [0,1,2,3],
    [0,1,2,3,4],
    [0,1]
];

如何使用lodash从结束或开始删除N个项目?

例如,如果我从头开始删除6个元素,我希望结果为:

var arr = [
    [1,2,3],
    [0,1,2,3,4],
    [0,1]
];

如果我从结尾删除1,我需要结果:

var arr = [
    [0,1,2,3,4],
    [0,1,2,3],
    [0,1,2,3,4],
    [0]
];

我希望我很清楚。 Lodash不是必需的。

这是我的代码:

function removeFromTop(group, count) {
    for (var i = 0; i < group.length; i++) {
        for (var x = 0; x < group[i].chatItems.length; x++) {
            if(count) {
                group[i].chatItems.splice(x, 1);
                if(!group[i].chatItems.length) {
                    group.splice(i, 1);
                };
                count--;
            } else {
                break;
            }
        };
    };
    return group;
}
function removeFromBottom(group, count) {
    for (var i = group.length - 1; i >= 0; i--) {
        for (var x = group[i].chatItems.length - 1; x >= 0; x--) {
            if(count) {
                group[i].chatItems.splice(x, 1);
                if(!group[i].chatItems.length) {
                    group.splice(i, 1);
                };
                count--;
            } else {
                break;
            }
        };
    };
    return group;
}

3 个答案:

答案 0 :(得分:2)

您可以从头开始为每个项目计数移动内部数组,并从末尾弹出值。对于第一个,您可以使用Array#reduce而另一个Array#reduceRight

&#13;
&#13;
function removeFromStart(array, n) {
    var copy = JSON.parse(JSON.stringify(array));
    return copy.reduce(function (r, a) {
        while (n && a.length) {
            a.shift();
            n--;
        }
        a.length && r.push(a);
        return r;
    }, []);
}

function removeFromEnd(array, n) {
    var copy = JSON.parse(JSON.stringify(array));
    return copy.reduceRight(function (r, a) {
        while (n && a.length) {
            a.pop();
            n--;
        }
        a.length && r.push(a);
        return r;
    }, []).reverse();
}

var array = [[0, 1, 2, 3, 4], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1]];

console.log(JSON.stringify(removeFromStart(array, 6)));
console.log(JSON.stringify(removeFromEnd(array, 6)));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用Lodash函数.drop 你可以删除一个数组的第一个元素,否则可以指定n个元素默认值为1.同样的方式 .dropRight表示结束元素。

&#13;
&#13;
var arr = [
    [0,1,2,3,4],
    [0,1,2,3],
    [0,1,2,3,4],
    [0,1]
];
// remove 1 element front  of 2D Array
var resultFront= arr.map(function(value,index) { return _.drop(value); });

console.log(resultFront);

// remove 1 element from End of 2D Array
var resultEnd= arr.map(function(value,index) { return _.dropRight(value); });

console.log(resultEnd);

// remove 1 element front and end  of 2D Array
var resultFrontEnd = arr.map(function(value,index) { return _.dropRight(_.drop(value)); });

console.log(resultFrontEnd);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

&#13;
&#13;
var arr = [
    [0,1,2,3,4],
    [0,1,2,3],
    [0,1,2,3,4],
    [0,1]
];
console.log(arr);
console.log('------------------------');
// remove 1 element front  of 2D Array
var resultFront= arr.map(function(value,index) { return _.drop(value); });
console.log('Remove 1 element from front') ;
console.log(resultFront);

// remove 1 element from End of 2D Array
var resultEnd= arr.map(function(value,index) { return _.dropRight(value); });
console.log('Remove 1 element from end') ;
console.log(resultEnd);

// remove 1 element front and end  of 2D Array
var resultFrontEnd = arr.map(function(value,index) { return _.dropRight(_.drop(value)); });

console.log('Remove 1 element from front & End') ;
console.log(resultFrontEnd);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以按照以下步骤操作;

var arr = [[0,1,2,3,4],[0,1,2,3],[0,1,2,3,4],[0,1]],
      r = [],
      n = 6,
    res = arr.reduce((r,sa) => r.n > sa.length ? (r.n -= sa.length, r)
                                               : (r.push(sa.slice(r.n)), r.n = 0, r), (r.n = n, r));
console.log(res);

我在reduce操作中的初始数组中使用状态变量r.n。您可能会或可能不会选择在之后将其删除。