我知道以前有类似的问题,但我试过每个答案,但没有为我工作。我在buttons
的的每次点击上创建我的array
。所以它看起来像这样:从元素可能不同的数组中删除重复的元素
<button type="button" name="10208823390691752,1317727711586522" value="All contacts" class="btn btn-default ognjen">All contacts</button>
<button type="button" name="10207252567926988,1294280923934896" value="Men" class="btn btn-default ognjen">Men</button>
<button type="button" name="10208823390691752,10207252567926988" value="Women" class="btn btn-default ognjen">Women</button>
<button type="button" name="1317727711586522,1294280923934896" value="Segment 1" class="btn btn-default ognjen">Segment 1</button>
所以这是我设法使一个阵列,所有的值点击要素:
$(document).ready(function() {
var clickedButtons = new Array();
var numUsers= new Array();
$('button.ognjen').click(function() {
var index = clickedButtons.indexOf(this.value);
if (index === -1){
clickedButtons.push(this.value);
numUsers.push(this.name);//value not found so push it
}else {
clickedButtons.splice(index, 1);
numUsers.splice(this.name);// value found so remove it
}
var tryIt=numUsers.join();
var picker=tryIt.split(', ');
console.log(picker);
});
所以picker
现在是一个数组,可能看起来像这样,后点击某些按钮:
["10207252567926988,1294280923934896,10208823390691752,1317727711586522,1294280923934896"]
现在,我想删除此数组中的所有重复元素。从这些问题的答案尝试:
- How to remove duplicates from array - javascript
- Easiest way to find duplicate values in a JavaScript array
- Remove duplicate elements from array using JavaScript
- Remove Duplicates from JavaScript Array
而且没有一次成功。我认为这可能是由于动态制作这个piker
阵列。请帮忙,我一整天都在为这个问题苦苦挣扎。
'numUsers.splice(this.name)每次重新创建阵列;'应'numUsers.splice(index,1);' –
这是一个只有1个元素的数组(一个有很多数字的字符串),所以删除重复项将会很困难,因为没有任何。 – Craicerjack
没有按钮名称 –