简体   繁体   中英

Loop through Array of Objects and append the Object to an Array if it doesn't exist

Desired Functionality: On selecting a checkbox, a span is created with an id & data attribute same as the checkbox and appended to a div. On clicking the 'x' on this span should uncheck the checkbox and remove the span as well.

Issue: On selecting the checkbox, an additional span with an 'undefined' label is created. 在此处输入图像描述

JSFIDDLE

  var filtersApplied = [];
  $('.ps-sidebar').on('change', 'input[type=checkbox]', function () {
  var me = $(this);
  console.log('me', me);
  if (me.prop('checked') === true) {
    filtersApplied.push([
      ...filtersApplied,
      { id: me.attr('id'), data: me.attr('data-filter-label') }
    ]);
  } else {
    filtersApplied = filtersApplied.map(function (item, index) {
      return item.filter(function (i) {
        return i.id !== item[index].id;
      });
    });
  }

  if (filtersApplied.length === 0) {
    $('.ps-plans__filters').hide();
    $('.ps-plans__filters-applied').html('');
  } else {
    $('.ps-plans__filters').show();
    var filtersAppliedHtml = '';
    filtersApplied.map(function (elements) {
      console.log('items', elements);
      return elements.map(function (el, i) {
        console.log('item', el);
        return (filtersAppliedHtml +=
          '<span class="ps-plans__filter" id="' + el.id + '_' + i +'">' +el.data +
          '<span class="icon-remove-circle remove-filter" data-filter="' +el.data +'"> X</span></span>');
      });
    });
    console.log('filtersAppliedHtml', filtersAppliedHtml);
    console.log($('.ps-plans__filters-applied').html(filtersAppliedHtml));
  }
});

Your undefined label is because of the ...filtersApplied

  if (me.prop('checked') === true) {
    filtersApplied.push([
      //this ...filtersApplied
      { id: me.attr('id'), data: me.attr('data-filter-label') }
    ]);

Note that filtersApplied is an array and you're making a push() , this method inserts a value in the end of the array, so your ...filtersApplied makes no sense. Just remove it and you'll be fine. You can se more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

there are few thing that need to be fixed.

  • when adding an element you should not push filtersApplied along with new object. instead you better do arr = [...arr, obj] ;
  • when remove an item you could apply a filter instead based on me.attr('id') . with map you would get undefined values;
  • after that you would map only once to build your html content, not twice;

var filtersApplied = [];
$('.ps-sidebar').on('change', 'input[type=checkbox]', function () {
  var me = $(this);
  if (me.prop('checked') === true) {
    filtersApplied = [
      ...filtersApplied,
      { id: me.attr('id'), data: me.attr('data-filter-label') }
    ];
  } else {
    filtersApplied = filtersApplied.filter(function (item, index) {
      return me.attr('id') !== item.id;
    });
  }

  if (filtersApplied.length === 0) {
    $('.ps-plans__filters').hide();
    $('.ps-plans__filters-applied').html('');
  } else {
    $('.ps-plans__filters').show();
    var filtersAppliedHtml = '';
    filtersApplied.map(function (el, i) {
        return (filtersAppliedHtml +=
          '<span class="ps-plans__filter" id="' +
          el.id +
          '_' +
          i +
          '">' +
          el.data +
          '<span class="icon-remove-circle remove-filter" data-filter="' +
          el.data +
          '">  X</span></span>');
    });
    
    $('.ps-plans__filters-applied').html(filtersAppliedHtml);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM