admin管理员组文章数量:1323317
I have a function which grabs a unique number from a data attribute in HTML. In my case it's called data-key which will have a unique number.
Now when a button is clicked next to HTML element that has a data-key, that key number is pushed into an Array.
I don't want the same number put into the array more than once.
Is there a simple way to acplish this? The way I'm imagining doing it is to check to see if that element is already in the Array first before pushing, but I feel like there is a simpler way?
My Codepen:
Click on the button to add an input value into the array.
< 6-1 gets added twice
jQuery
var deletedArray = [];
$('.remove_info').unbind('click').bind("click", function(event) {
//console.log('clicked remove x');
var value = $(this).prev().attr('value');
var key = $(this).prev().data('key');
deletedArray.push(key);
console.log('this value = '+value+' deletedArray = '+deletedArray);
});
I have a function which grabs a unique number from a data attribute in HTML. In my case it's called data-key which will have a unique number.
Now when a button is clicked next to HTML element that has a data-key, that key number is pushed into an Array.
I don't want the same number put into the array more than once.
Is there a simple way to acplish this? The way I'm imagining doing it is to check to see if that element is already in the Array first before pushing, but I feel like there is a simpler way?
My Codepen: http://codepen.io/leongaban/pen/CEfxi
Click on the button to add an input value into the array.
< 6-1 gets added twice
jQuery
var deletedArray = [];
$('.remove_info').unbind('click').bind("click", function(event) {
//console.log('clicked remove x');
var value = $(this).prev().attr('value');
var key = $(this).prev().data('key');
deletedArray.push(key);
console.log('this value = '+value+' deletedArray = '+deletedArray);
});
Share
asked Oct 9, 2013 at 17:23
Leon GabanLeon Gaban
39.1k122 gold badges349 silver badges550 bronze badges
2
-
What's the purpose of
.unbind('click').bind("click",...
? – j08691 Commented Oct 9, 2013 at 17:25 -
Perhaps modify the DOM to flag that element as already having its value inserted and check that flag before inserting e.g. add
data-inserted=true
. – Eric J. Commented Oct 9, 2013 at 17:25
2 Answers
Reset to default 8Should've been a ment but put a control statement before the push function call
if (deletedArray.indexOf(key) < 0) deletedArray.push(key);
For browser patibility jQuery's inArray
if ($.inArray(key, deletedArray) < 0) deletedArray.push(key);
Note: indexOf
is not patible with IE 8 and earlier
If all elements in your array must be unique, then I would really remend using Underscore's array union:
arr = ["1", 2, "apple"];
console.log(_.union(arr, 3)); //["1", 2, "apple", 3]
console.log(_.union(arr, 2)); //["1", 2, "apple"]
本文标签: javascriptHow to prevent pushing value into Array twice (jQuery)Stack Overflow
版权声明:本文标题:javascript - How to prevent pushing value into Array twice? (jQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742144021a2422709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论