过滤数组以具有唯一值[重复]
Posted
技术标签:
【中文标题】过滤数组以具有唯一值[重复]【英文标题】:Filter array to have unique values [duplicate] 【发布时间】:2016-11-07 11:54:34 【问题描述】:我需要过滤掉我的数组以仅包含唯一值。 这是我的数组数据
["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
预期结果应该是
["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8", "X_row9"]
我应该如何继续我的代码以获得正确的结果。
newArray = [];
for(n in data)
if(!newArray.indexOf(n))
newArray.push(n);
console.log(newArray);
如果您需要任何其他信息,请告诉我,我会提供。谢谢
【问题讨论】:
["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr) return i==arr.indexOf(v); )
请仔细检查您的预期输出 - 为什么不包括 X_row9
?
使用循环:var x = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4" 、“X_row6”、“X_row10”、“X_row8”、“X_row9”、“X_row11”、“X_row7”、“X_row4”、“X_row6”、“X_row10”、“X_row8”、“X_row9”、“X_row11”、“ X_row7”、“X_row4”、“X_row6”、“X_row10”、“X_row8”、“X_row9”、“X_row11”、“X_row7”、“X_row4”、“X_row6”、“X_row10”、“X_row8”、“X_row9” ,“X_row11”,“X_row7”,“X_row4”,“X_row6”,“X_row10”];让它 =[]; for (let i = 0; i
【参考方案1】:
indexOf
方法的细微变化,如果您需要过滤多个数组:
function unique(item, index, array)
return array.indexOf(item) == index;
这样使用:
arr.filter(unique);
【讨论】:
【参考方案2】:您可以使用Array.filter 函数根据回调函数的返回值过滤掉数组中的元素。回调函数针对原始数组的每个元素运行。
这里回调函数的逻辑是,如果当前项的indexOf
值与索引相同,则表示该元素是第一次遇到,因此可以认为是唯一的。如果没有,则表示该元素已经遇到过,所以现在应该丢弃。
var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];
var filteredArray = arr.filter(function(item, pos)
return arr.indexOf(item)== pos;
);
console.log( filteredArray );
警告:正如 cmets 中的 rob 所指出的,这种方法应该避免使用非常大的数组,因为它在 O(N^2)
中运行。
更新(2017 年 11 月 16 日)
如果您可以依赖ES6 features,那么您可以使用Set object 和Spread operator 从给定数组创建一个唯一数组,正如下面@Travis Heeter 的回答中所述:
var uniqueArray = [...new Set(array)]
【讨论】:
原始的 ES5 解决方案是 O(N^2),所以如果你使用它,请确保你的数组不要太大。【参考方案3】:据我所知,这适用于es2015
及以上。 ES6
有“更清洁”的选项,但这是一个很好的方法(TypeScript
)。
let values: any[] = [];
const distinct = (value: any, index: any, self: any) =>
return self.indexOf(value) === index;
;
values = values.filter(distinct);
【讨论】:
【参考方案4】:Array.prototype.unique = function ()
return [...new Set(this)]
那么我们可以写:
const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
const uniqueArr = arr.unique()
【讨论】:
不错的简洁解决方案,效果很好!当我尝试Array.prototype.unique = () => [...new Set(this)];
时,我得到"Uncaught (in promise) TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))"
。有任何想法吗?谢谢!
因为this
使用箭头函数时引用window
,而使用普通函数时引用数组。【参考方案5】:
可以使用 javascript Set 和 Array.from 方法来过滤数组以包含唯一值,如下所示:
Array.from(new Set(arrayOfNonUniqueValues));
Set
Set 对象允许您存储任何类型的唯一值,无论是 原始值或对象引用。
返回值一个新的 Set 对象。
Array.from()
Array.from() 方法从一个 类数组或可迭代对象。
返回值一个新的 Array 实例。
示例代码:
const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
const uniqueArray = Array.from(new Set(array));
console.log("uniqueArray: ", uniqueArray);
【讨论】:
【参考方案6】:我一直用:
unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);
但最近我不得不为:
["1", 1, "2", 2, "3", 3]
而我的旧待机并没有削减它,所以我想出了这个:
uunique = (arr) => Object.keys(Object.assign(, ...arr.map(a=>([a]:true))));
【讨论】:
【参考方案7】:您可以使用哈希表来查找和过滤所有未包含的值。
var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
unique = data.filter(function (a)
return !this[a] && (this[a] = true);
, Object.create(null));
console.log(unique);
【讨论】:
【参考方案8】:arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`$uniqueArr\n$reallyUniqueArr`)
【讨论】:
请提供代码上下文【参考方案9】:从 2015 年 6 月 15 日起,您可以使用 Set()
创建唯一数组:
var uniqueArray = [...new Set(array)]
你的例子:
var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
var newArray = [...new Set(data)]
console.log(newArray)
>> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
【讨论】:
【参考方案10】:您可以使用Map 和Spread Operator:
var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];
var unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
【讨论】:
【参考方案11】:您可以使用reduce
循环数组并获取不重复的值。还使用 aux object
来获取附加值的计数。
var aux = ;
var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>
if(!aux[curr])
aux[curr] = 1;
tot.push(curr);
return tot;
, []);
console.log(newArray);
【讨论】:
以上是关于过滤数组以具有唯一值[重复]的主要内容,如果未能解决你的问题,请参考以下文章
基于具有唯一值的数组创建多个动态选择过滤器以过滤 Vue.js 中的另一个数组
AngularJS NG-repeat:如何重复必须过滤唯一值的ng-repeat内的数据