[英]Remove duplicates from a multidimensional array
I have found questions that kind of touch on the issue I'm having, but I haven't found a solution that works for me yet. 我发现了与我遇到的问题有关的问题,但我还没有找到适合我的解决方案。 I have this array:
[[1, red], [2, green], [3, red], [3, blue], [5, green]]
and I need it to return [[1, red], [2, green], [3, blue]
. 我有这个数组:
[[1, red], [2, green], [3, red], [3, blue], [5, green]]
,我需要它返回[[1, red], [2, green], [3, blue]
。 What I need the code to do is go through the array and find ONLY colors that match, not numbers, and get rid of that entire index. 我需要代码执行的操作是遍历数组,仅查找匹配的颜色,而不是数字,并消除整个索引。
I have tried something like this 我已经尝试过这样的事情
var uniqueArray = colors.filter(function(item, pos) {
return colors.indexOf(item) == pos;
});
I'm thinking that this code is searching for a complete match, and I only require a partial match. 我认为这段代码正在搜索完全匹配,而我只需要部分匹配。 So basically, how would I modify
.filter()
to get rid of partial duplicates (only matching the colors)? 因此,基本上,我将如何修改
.filter()
来消除部分重复(仅匹配颜色)?
Please let me know if I need to provide any more information. 如果需要提供更多信息,请告诉我。
I would use a for
loop to populate a new, unique array: 我将使用
for
循环来填充新的唯一数组:
var old_array = [[1, red], [2, green], [3, red], [3, blue], [5, green]],
old_count = old_array.length,
unique_array = [], check_array = [], i = 0;
for(; i < old_count; i++) {
if (check_array.indexOf(old_array[i][1]) === -1) {
check_array.push(old_array[i][1]);// this array is filled with new colors, and used for checking
unique_array.push(old_array[i]);
}
}
You could use a hash table with the color and use Array#filter
for the wanted items. 您可以使用具有颜色的哈希表,并对所需项目使用
Array#filter
。
var data = [[1, 'red'], [2, 'green'], [3, 'red'], [3, 'blue'], [5, 'green']], result = data.filter(function (a) { if (!this[a[1]]) { return this[a[1]] = true; } }, Object.create(null)); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
// Parameter marr: multidimensional array
function removeSameColors(marr){
var carr = [];
var rarr = [];
var j = -1;
for(var i = 0, l = marr.length; i < l; i++){
if(carr[marr[i][1]] !== true){
carr[marr[i][1]] = true;
rarr[++j] = marr[i];
}
}
return rarr;
}
That should solve your problem with very low execution time. 那应该以非常低的执行时间解决您的问题。
I would just keep track of the unique colors in an object passed into the filter, because as a hash it's guaranteed to be unique. 我只是跟踪传递到滤镜中的对象中的唯一颜色,因为作为哈希,它保证是唯一的。 If the object doesn't have a property by that color name, it returns it from the filter.
如果对象不具有该颜色名称的属性,它将从过滤器返回它。 Otherwise if it does it ignores the item.
否则,它将忽略该项目。
var colors = [[1, "red"], [2, "green"], [3, "red"], [3, "blue"], [5, "green"]];
var uniqueArray = colors.filter(function(item, pos) {
if (!this.hasOwnProperty(item[1])) {
return this[item[1]] = true;
}
return false;
}, {});
This gives you uniqueArray = [[1,"red"],[2,"green"],[3,"blue"]]
, as expected. 如预期的那样,这将为您提供
uniqueArray = [[1,"red"],[2,"green"],[3,"blue"]]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.