Remove specific array from multiple array set using jquery

Related searches

I have two checkboxes. When I check one, the code will get status and id of that checkbox and push into array, if that value is not present already

The array set become like this

  [8,0] [10,0]

Requirements:

  1. It is inserting [8,0] [8,0] twice if I check and then uncheck and again check it so this should not insert multiple times same values
  2. Remove specific array from set of array so if I uncheck chkecbox then remove only [8,0] but keep [10,0]

var positions = [];

$("body").on('click', '.check_box', function() {
  var id = $(this).attr('data-id');
  var status = $(this).attr('data-status');
  if ($(this).prop("checked") == true) { // if click on check
    if (!$.inArray(id, positions)) positions.push(id); // if id and status not in array then only push
    positions.push([id, status]); //  it will  insert  like [8,10] but geting duplicate  [8,10] [8,10]
    console.log(positions);
  } else {
    // if uncheck checkbox

    //  remove specific  value like  [8,0] or if value present [10,0] then remove this
    console.log(positions);
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check_box" data-id="8" data-status="0">

<input type="checkbox" class="check_box" data-id="10" data-status="0">

You can use indexOf to check if object is present in array & add only if it doesn't exist

For removing, you can use filter & select only those objects in array which are not exactly as you specify

var positions = [];

$("body").on('click', '.check_box', function() {
  var id = $(this).attr('data-id');
  var status = $(this).attr('data-status');
  if ($(this).prop("checked") == true) {
    var exists = false;
    positions.forEach((p) => {
      if (id == p[0] && status == p[1]);
        exists = true;
    });
    if (!exists) {
      positions.push([id, status]);
    }  
  } else {
    positions = positions.filter(function(a) {
      return !(a[0] == id && a[1] == status);
    });
  }
  console.log(positions);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check_box" data-id="8" data-status="0">

<input type="checkbox" class="check_box" data-id="10" data-status="0">

How to remove multiple elements from array in JavaScript , Approach 1: Store the index of array elements into another array which need to be removed. Start a loop and run it to the number of elements in the array. Use splice() method to remove the element at a particular index. I have an array that looks like this: var y = [1, 2, 3]; I would like to remove 2 from array y.. How can I remove a particular value from an array using jQuery? I have tried pop() but that always removes the last element.


I will use findIndex and splice to handle it, hope this can help you :)

$(function() {
    let positions = [];
    $("body").on('click', 'input:checkbox', function() {
        let id = $(this).attr('data-id');
        let status = $(this).attr('data-status');
        let data = [id, status];
        if ($(this).is(':checked')) {
            positions.push(data);
        } else {
            let index = positions.findIndex(c => c[0] === id);
            if (index != -1)
                positions.splice(index, 1);
        }
        $('#result').html(positions.toString());
    });
});
#result{
  width: 20rem;
  height: 1rem;
  margin-top: 2rem;
  border-width:3px;
  border-style:dashed;
  border-color:#FFAC55;
  padding: 15px 20px;
}
<!DOCTYPE html>
<html>

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
    <input type="checkbox" data-id="A" data-status="0">A</button>
    <input type="checkbox" data-id="B" data-status="0">B</button>
    <div id="result"></div>
</body>

</html>

9 Ways To Remove 🗑 Elements From A JavaScript Array , So, how do you delete an element from a JavaScript array? JavaScript Array elements can be removed from the end of an array by setting the If you want to remove multiple items that match your criteria there is a glitch. jQuery: Remove a specific value from an array using jQuery Last update on February 26 2020 08:07:52 (UTC/GMT +8 hours)


If the array is small, just recreate it each time

let positions;
$("body").on('click', '.check_box', function() {
  positions = [];
  $(".check_box:checked").each(function() {
    positions.push([
     $(this).data('id'), $(this).data('status')
   ]);
  });
  console.log(positions);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check_box" data-id="8" data-status="0">

<input type="checkbox" class="check_box" data-id="10" data-status="0">

How to Remove Elements from an Array in JavaScript, How to use length property, delete operator, and pop, shift, and splice methods to You can also remove elements from the end of an array by setting the length� Store the index of array elements into another array which need to be removed. Use filter() method on the array of elements. Use indexOf() method to select only those elements which are not present in the indexes array. Example 2: This example uses filter() method and indexOf() method to remove multiple elements from array.


How to delete a value from an array in JavaScript, And this is precisely why things like jQuery exist. Every other modern programming language has Add and Remove mothods, clearly and logically named. But, of� Explicitly Remove Array Elements Using the Delete Operator; Clear or Reset a JavaScript Array; Summary; There are different methods and techniques you can use to remove elements from JavaScript arrays: pop - Removes from the End of an Array; shift - Removes from the beginning of an Array; splice - removes from a specific Array index


JavaScript Array splice(): Delete, Insert, and Replace Elements in an , However, you can use this method to delete and replace existing elements as well. To delete elements in an array, you pass two arguments into the splice() method as You can replace one element with multiple elements by passing more� jquery push array with key, create array with key and value in jquery, javascript array push key value pair dynamically, array push with specific key javascript, javascript array push dynamic key value


Be careful when you use delete for an array. It is good for deleting attributes of objects, but not so good for arrays. It is better to use splice for arrays. Keep in mind that when you use delete for an array you could get wrong results for anArray.length.