[英]Remove specific array from multiple array set using jquery
我有兩個復選框。 當我選中一個時,代碼將獲取該復選框的狀態和 id 並推送到數組中,如果該值尚不存在
數組集變成這樣
[8,0] [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">
您可以使用indexOf
檢查對象是否存在於數組中並僅在它不存在時添加
對於刪除,您可以使用filter
並僅選擇數組中與您指定的不完全相同的那些對象
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">
解釋
在第一個循環.each
,當我們找到一個與我們選擇的元素具有相同 id 和狀態的元素時,我們遍歷數組中的每個現有值並將exist
設置為 true
在 else 條件下,我們使用了數組的filter
函數,這個函數過濾了數組並只保留我們返回 true 的元素,對於我們返回 false 的元素,它會從結果數組中刪除
id
和status
完全匹配,如果匹配,則返回 false,因此它會從結果數組中刪除我將使用findIndex和splice來處理它,希望這可以幫助你:)
$(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>
如果數組很小,每次只需重新創建它
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">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.