Ognj3n
Ognj3n

Reputation: 759

Removing duplicate elements from array whose elements can vary

I know similar questions have been asked before, but I tried every answer and nothing worked for me. I am creating my array on every click on some of buttons with class ognjen. So it looks like this:

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

So this is how I managed to make one array with values of all clicked elements:

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

So picker is now an array that may look like this, after clicking certain buttons:

["10207252567926988,1294280923934896,10208823390691752,1317727711586522,1294280923934896"]

Now, I would like to remove all duplicate elements from this array. Tried answers from these questions:

And none of them worked. I think it may be due to dynamics of making this piker array. Please help, I'm struggling whole day with this problem.

Upvotes: 0

Views: 77

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

I think an easier approach will be store the clicked state of each button then recreate the array each time like

$(document).ready(function() {

  var clickedButtons = new Array();
  var numUsers = new Array();
  var $btns = $('button.ognjen').click(function() {
    $(this).toggleClass('selected');

    clickedButtons = new Array();
    $btns.filter('.selected').each(function() {
      var values = this.value.split(',');
      values.forEach(function(value) {
        var index = clickedButtons.indexOf(value);
        if (index === -1) {
          clickedButtons.push(value);
        }
      });
    });

    console.log(clickedButtons)
    snippet.log(clickedButtons.join() || 'NONE');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<button type="button" value="10208823390691752,1317727711586522" class="btn btn-default ognjen">All contacts</button>
<button type="button" value="10207252567926988,1294280923934896" class="btn btn-default ognjen">Men</button>
<button type="button" value="10208823390691752,10207252567926988" class="btn btn-default ognjen">Women</button>
<button type="button" value="1317727711586522,1294280923934896" class="btn btn-default ognjen">Segment So

Upvotes: 1

Related Questions