我正在尝试查找在Javascript数组中只出现一次的项目。在以下数组中:
['txfa2','txfa9','txfa2','txfa1','txfa3','txfa4','txfa8','txfa9','txfa2','txfa8']
结果应为:
['txfa1','txfa3','txfa4']
我目前在jQuery和.each()
中使用.sort()
函数。这样做有更聪明或更好的方法吗?你知道任何jQuery插件可以用更少的代码行完成。
<script src="../../js/jq.js"></script>
<script>
var items = ['txfa2', 'txfa9', 'txfa2', 'txfa1', 'txfa3', 'txfa4', 'txfa8', 'txfa9', 'txfa2', 'txfa8'];
var nopairs = []; //should contain only txfa1, txfa3, txfa4
var haspair = '';//contains the item which has pairs
var haspair_ctr = 0;
var nopair_ctr = 0;
var arranged = items.sort();
$(arranged).each(function(index){
if(index != arranged.length){
if(arranged[index] != arranged[index + 1] && arranged[index] != haspair){
nopairs[nopair_ctr] = arranged[index];
nopair_ctr++;
}else{
haspair = arranged[index];
}
}
});
console.log(nopairs);
</script>
答案 0 :(得分:5)
以下是使用ES5函数方法的示例,基于使用对象计算每个值出现的次数:
function uniq(a) {
// create a map from value -> count(value)
var counts = a.reduce(function(o, k) {
o[k] = o[k] ? o[k] + 1 : 1;
return o;
}, {});
// find those that only appeared once
return Object.keys(counts).filter(function(k) {
return (counts[k] === 1);
});
}
的工作演示
答案 1 :(得分:4)
一种简单的方法是利用Javascript的内置对象。让一个对象充当集合中每个项目的计数器,然后迭代它以检查哪个项目的计数器为1。
它非常快;
var items = ['txfa2', 'txfa9', 'txfa2', 'txfa1', 'txfa3', 'txfa4', 'txfa8', 'txfa9', 'txfa2', 'txfa8']
, result = []
, i
, k
, container = {};
for (i = 0; i < items.length; ++i) {
if (items[i] in container) {
container[items[i]]++;
} else {
container[items[i]] = 1;
}
}
for (k in container) {
if (container[k] == 1) {
result.push(k);
}
}
console.log(result)
答案 2 :(得分:4)
简明扼要的方法:
function singles( array ) {
for( var index = 0, single = []; index < array.length; index++ ) {
if( array.indexOf( array[index], array.indexOf( array[index] ) + 1 ) == -1 ) single.push( array[index] );
};
return single;
};
演示:http://jsfiddle.net/ThinkingStiff/C849F/
以下是显示此方法(蓝色)的此问题(实际上有效)的非重复答案的性能与其他方法相当。
效果:http://jsperf.com/find-array-singles/3
答案 3 :(得分:1)
通过“找到独特的项目”我相信你的意思是“找到不重复的项目”(与“查找不同的值”相比)?此外,我不明白为什么您的haspair
变量是一个字符串:您的示例数据中包含多个对。总之...
有很多方法可以做到这一点,但我会使用一个对象来计算每个不同的值。这使得生成非重复项的数组和重复项的数组以及不同值的数组变得容易。显然,如果你不需要这三个你省略了你不关心的那些,但我已经在下面显示了所有这三个,这就是为什么它看起来可能比你想要的长一些。当然,如果您想要计算任何类别中的数量,您只需使用数组长度。
var items = ['txfa2', 'txfa9', 'txfa2', 'txfa1', 'txfa3', 'txfa4', 'txfa8', 'txfa9', 'txfa2', 'txfa8'];
var working = {},
hasPairs = [],
noPairs = [],
distinct = [],
i, k;
for (i=0; i < items.length; i++)
if (working.hasOwnProperty(items[i]))
working[items[i]]++;
else
working[items[i]] = 1;
for (k in working) {
if (working[k] > 1)
hasPairs.push(k);
else
noPairs.push(k);
distinct.push(k);
}
注意:我使用普通的JavaScript编写了上述代码,但未使用旧版浏览器可能不支持的较新的数组函数。显然,您可以使用基本算法并使用jQuery迭代初始数组和/或working
的属性,或者如果您不关心IE,则可以使用.forEach()
&lt; 9,等等。
答案 4 :(得分:-1)
/**
* Array.uniq();
*
* @author: Alexander Guiness
* @param: {Array}
* @return: {Array} An array of unique values
* @licence: MIT
* @use: Array.uniq([1,1,1,1,2,2,2,4,5,5,4]); //1,2,4,5
* @date: Mon Jul 26 10:00:00 2011
*/
(function($) {
'use strict';
if(!$.uniq) {
$.uniq = function(array) {
if(Object.prototype.toString.call(array) !== '[object Array]')
return -1;
var i = array.length;
array.sort();
while(i--) {
if(array[i] == array[i-1]) {
array.splice(i, 1);
}
}
return array;
}
}
}(Array));
请参阅example
或使用:jQuery.unique([]);