过滤掉数组以仅包含唯一值

时间:2016-07-05 15:08:16

标签: javascript arrays

我需要过滤掉我的数组以仅包含唯一值。 这是我的数组数据

["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]

预期结果应为

newArray = [];
for(n in data){
  if(!newArray.indexOf(n)){
     newArray.push(n);
  }
}
console.log(newArray);

我应该如何继续使用我的代码才能获得正确的结果。

defaults write com.google.Chrome ExtensionInstallSources -array "http://install-url.com/*"
defaults write com.google.Chrome ExtensionInstallWhitelist -array "chrome-extension-id"

如果您需要任何其他信息,请告诉我,我会提供。谢谢

11 个答案:

答案 0 :(得分:44)

您可以使用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 );

警告:正如评论中rob所指出的那样,在O(N^2)中运行非常大的数组时,应该避免使用此方法。

更新(2017年11月16日)

如果您可以依赖ES6 features,那么您可以使用Set objectSpread operator从给定数组创建一个唯一数组,如@Travis Heeter在下面的答案中所述:< / p>

var uniqueArray = [...new Set(array)]

答案 1 :(得分:15)

截至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"]

答案 2 :(得分:6)

您可以使用哈希表进行查找并过滤所有未包含的值。

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);

答案 3 :(得分:6)

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}`)

答案 4 :(得分:6)

使用JavaScript 设置 Array.from 方法过滤数组以包含唯一值,如下所示:

Array.from(new Set(arrayOfNonUniqueValues));

<强> Set

  

Set对象允许您存储任何类型的唯一值,无论是否   原始值或对象引用。

     

返回值新的Set对象。

<强> Array.from()

  

Array.from()方法从中创建一个新的Array实例   类似数组或可迭代的对象。

     

返回值新的Array实例。

示例代码:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 5 :(得分:4)

您可以使用MapSpread Operator

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 6 :(得分:3)

您可以使用reduce循环数组并获取不重复的值。还使用辅助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);

答案 7 :(得分:2)

indexOf方法略有不同,如果您需要过滤多个数组:

function unique(item, index, array) {
    return array.indexOf(item) == index;
}

使用原样:

arr.filter(unique);

答案 8 :(得分:2)

我一直用:

Access to path [filepath] is denied

但最近我必须得到唯一的值:

unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);

我的旧待机没有削减它,所以我提出了这个:

["1", 1, "2", 2, "3", 3]

答案 9 :(得分:2)

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()

答案 10 :(得分:2)

据我所知,这是针对es2015及更高版本的。 ES6有“更清洁”的选项,但这是一种很好的选择(TypeScript)。

let values: any[] = [];
const distinct = (value: any, index: any, self: any) => {
    return self.indexOf(value) === index;
};
values = values.filter(distinct);