我有以下JS,我试图找出另一个2D数组中是否包含2D数组的元素。但是,包括似乎无法正常工作,对于循环中的每个索引,我都会将不匹配记录到控制台。我该如何使用2D阵列呢?
deleteLinks.on('click', function(ev){
ev.preventDefault();
var currentHTML = $('.product');
var currentHTMLMap = $('.product').toArray().map(elm => [ $(elm).attr("data-id-product"), $(elm).attr("data-id-product-attribute") ]);
var deleteIndices = [];
$.ajax({
url: this.href,
type: "GET",
dataType: "html",
success: function(data) {
var newHTMLMap = $(data).find(".product").toArray().map(elm => [ $(elm).attr("data-id-product"), $(elm).attr("data-id-product-attribute") ]);
for(i = 0; i < currentHTMLMap.length; i++){
if (!(newHTMLMap.includes(currentHTMLMap[i]))) {
console.log("found mismatch for index " + i);
deleteIndices.push(i);
}
}
for(i = 0; i < deleteIndices.length; i++) {
currentHTML[deleteIndices[i]].remove();
}
}
});
});
我在变量内获得以下数据:
currentHTMLMap: 6,0,2,9,1,1
currentHTMLMap[0]: 6,0
currentHTMLMap[1]: 2,9
currentHTMLMap[2]: 1,1
newHTMLMap: 2,9,1,1
newHTMLMap[0]: 2,9
newHTMLMap[1]: 1,1
答案 0 :(得分:1)
您可以使用Array#findIndex
进行检查,而元素可以使用Array#every
进行检查。
var array = [[6, 0], [2, 9], [1, 1]],
subset = [[2, 9], [1, 1], [1, 2], [4, 5]];
console.log(subset.map(a => array.findIndex(b => a.every((v, i) => v === b[i]))));