查找在Javascript数组中只出现一次的项目

我正在尝试查找在Javascript数组中只出现一次的项目。 在以下数组中:

['txfa2','txfa9','txfa2','txfa1','txfa3','txfa4','txfa8','txfa9','txfa2','txfa8'] 

结果应该是:

 ['txfa1','txfa3','txfa4'] 

我目前在jQuery和.sort()使用.each()函数。 这样做有更聪明或更好的方法吗? 你知道任何jQuery插件可以用更少的代码行完成。

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

一个简洁的方法:

 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

在此处输入图像描述

这是使用ES5的function方法的示例,基于使用对象来计算每个值发生的次数:

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

http://jsfiddle.net/alnitak/shyce/的工作演示

一个简单的方法将利用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) 

通过“找到独特的项目”我相信你的意思是“找到不重复的项目”(与“查找不同的值”相比)? 另外,我不明白为什么你的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属性,或者你可以使用.forEach()如果你不关心IE <9等。

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

看到这个例子

或者使用: jQuery.unique([]);