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
Add a ment  | 

2 Answers 2

Reset to default 8

Should'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