代码之家  ›  专栏  ›  技术社区  ›  ItFreak

Javascript二维数组包含

  •  1
  • ItFreak  · 技术社区  · 6 年前

    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
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nina Scholz    6 年前

    你可以跟我核实一下 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]))));