简体   繁体   English

从特定的数组中删除数组中的元素

[英]Remove elements from array except particular one

I have two arrays. 我有两个数组。 First one is an array of indexes and second one is an array of objects. 第一个是索引数组,第二个是对象数组。 They look like this: 它们看起来像这样:

var nums = [0, 2];
var obj = [Object_1, Object_2, Object_3];

In this particular case I need to remove all " obj " elements except obj[0] and obj[2] . 在这种特殊情况下,我需要删除除obj[0]obj[2]之外的所有“ obj ”元素。 So the result will look like this: 所以结果看起来像这样:

obj = [Object_2]

There are also may be cases when nums = [0, 1, 2] and obj = [Object_1, Object_2, Object_3] ; nums = [0, 1, 2]obj = [Object_1, Object_2, Object_3]时,也可能出现这种情况; In that case I dont need to remove any elements. 在那种情况下,我不需要删除任何元素。

The " obj " length is always greater than "nums" length. obj ”长度始终大于“nums”长度。

So I started with finding only the elements that I need to save: 所以我开始只找到需要保存的元素:

nums.forEach(function(key) {
    obj.forEach(function(o, o_key) {
        if (key === o_key) {
            console.log(key, o);
            // deleting remaining elements
        }
    });
});

The question: How can I remove elements that dont meets my condition? 问题:如何删除不符合我条件的元素? I dont need the new array, I want to modify the existing "obj" array. 我不需要新的数组,我想修改现有的“obj”数组。 How can I achieve this functionality? 我该如何实现此功能? Or should I use some another techniques? 或者我应该使用其他技术吗?

You coudld check if the length of the indices is the same length of the object array and return or delete the objects at the given indices. 您可以检查索引的长度是否与对象数组的长度相同,并返回或删除给定索引处的对象。

It needs a sorted array for indices, because Array#splice changes the length of the array. 它需要一个排序的索引数组,因为Array#splice改变数组的长度。 (With an array with descending sorted indices, you could use Array#forEach instead of Array#reduceRight .) (对于具有降序排序索引的数组,您可以使用Array#forEach而不是Array#reduceRight 。)

 function mutate(objects, indices) { if (objects.length === indices.length) { return; } indices.reduceRight(function (_, i) { objects.splice(i, 1); }, null); } var objects = [{ o: 1 }, { o: 2 }, { o: 3 }]; mutate(objects, [0, 1, 2]); // keep all items console.log(objects); objects = [{ o: 1 }, { o: 2 }, { o: 3 }]; // delete items at index 0 and 2 mutate(objects, [0, 2]); console.log(objects); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

您可以使用过滤器执行此操作,假设nums是要保留的元素索引上的数组。

obj = obj.filter((o, i) => nums.indexOf(i) > -1); 

If you want to keep the same array object, you need to use splice eg in a simple reverse for in order to not mess the indices: 如果你想保持相同的数组对象,你需要使用splice例如在一个简单的反向for ,以便不混乱索引:

for (var i=obj.length-1; i>=0; i--) {
    if (nums.indexOf(i) < 0) {
        obj.splice(i, 1);
    }
}

This is assuming the list of indices (nums) is ordered. 这是假设索引(nums)列表是有序的。 If not, we first need to sort it: 如果没有,我们首先需要对它进行排序:

var sortedNums = nums.sort(function (a, b) {  return a - b;  });

And then use the sortedNums to check the indexOf 然后使用sortedNums检查indexOf

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

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