0

I have the following JavaScript that contains two objects with an id and an attribute.

How can I check if a specific combination (id + attr tupel) from A is included in B? so is currentHMLMap[i] included anywhere in the whole newHTMLMap object?

    deleteLinks.on('click', function(ev){
        ev.preventDefault();
        var currentHTML = $('.product');
        var currentId = $("body").find('.product').toArray().map(function(e){return $(e).attr("data-id-product");});    
        var currentAtr = $("body").find('.product').toArray().map(function(e){return $(e).attr("data-id-product-attribute");}); 
        currentHTMLMap = currentId.map(function(x, i) {
            return{"id": x, "atr" : currentAtr[i]} 
        }.bind(this));
        var newHTML ;
        var deleteIndices = [];
            $.ajax({
            url: this.href,
            type: "GET",
            dataType: "html",
              success: function(data) {
                  newHTMLId = $(data).find('.product').toArray().map(function(e){ return $(e).attr("data-id-product");})
                  newHTMLAtr = $(data).find('.product').toArray().map(function(e){ return $(e).attr("data-id-product-attribute");})     
                 //creating "map"
                 newHTMLMap = newHTMLId.map(function(x, i) {
                     return{"id": x, "atr" : newHTMLAtr[i]} 
                 }.bind(this));
                  for(i = 0; i  < currentHTML.length; i++){
// is currentHMLMap[i] included in newHTMLMap?
                      if (??){
                    deleteIndices.push(i);
                      }
                  }
                     for(i = 0; i < deleteIndices.length; i++) {
                    console.log("removing index" + deleteIndices[i]);
                    currentHTML[deleteIndices[i]].remove();
    }
              }
        });
    });

Update:

currentHTMLMap:

0: {id: 1, atr: 5}
1: {id: 3, atr: 71}

newHTMLMap:

0: {id: 3, atr: 71}

It is possible, that newHTMLMap contains more than one entry and there may be more than one element removed.

So the first index of currentHTMLMap is not included in the newHTMLMap, so I want to push 0 to deleteIndices.
I hope this helps for clarification.

ItFreak
  • 2,299
  • 5
  • 21
  • 45

2 Answers2

0

var a = [
{id: 1, atr: 5},
{id: 3, atr: 71}
]

var b = {id:1, atr: 7}

var found = false;
a.forEach(function(el){
  if(JSON.stringify(el) === JSON.stringify(b)){
    found = true;
  }
});

console.log(found);
If you need the indexes in case b had multiple elements:

var a = [
{id: 1, atr: 5},
{id: 3, atr: 71},
{id: 4, atr: 81},
{id: 5, atr: 91},
{id: 6, atr: 171}
]

var b = [
    {id:1, atr: 7},
    {id:1, atr: 5},
    {id: 5, atr: 91}
]

var found = false;
var matches = [];
a.forEach(function(aEl,aElIndex){
    b.forEach(function(bEl, bElIndex){
      if(JSON.stringify(aEl) === JSON.stringify(bEl)){
        matches.push({
            "aElIndex": aElIndex,
            "bElIndex": bElIndex
        })
      }      
    })
  
});

console.log(matches);
Lukasz
  • 1,807
  • 8
  • 12
  • That was my first approach, but I need to add the specific index in case of a mismatch to an array and it is possible that more than one element is missing. (or in other words: b may contain more than one entrie and more than one entry could have been removed) – ItFreak Oct 04 '18 at 09:34
  • Inside the first forEach iterate over the b. Save the matching indexes into an array. Once you have an array of indexes at wich you have to add or remove something, you can simply iterate over it and call splice or whatever you need. Would that work? – Lukasz Oct 04 '18 at 09:38
  • Yea that would probably work but this will give me 'false alarms' if I compare a[1] != b[1] but a[1] == b[2], wouldn' it? – ItFreak Oct 04 '18 at 09:40
  • I added the solution if you had more elements in b, have a look. It spits out an array of matches, you can do whatever you want with that, no? – Lukasz Oct 04 '18 at 09:49
  • Yea its almost what I need.I thought of iterating over the currentHTMLMap.lengthand then doing if(! newHTMLMap.includes(currentHTMLMap[i])) and then return the index of this iteration, but I have no arrays so I cant call includes(). Do you know what I mean? – ItFreak Oct 04 '18 at 09:59
0

Use somthing like this function containsObject()

var data = [
    {id: 1, atr: 5},
    {id: 3, atr: 71}
];
var data2 =[
    {id: 3, atr: 71}
];
function containsObject(obj, list) {
    var i;
    var x;
    for(x=0;x<obj.length;x++)
        for (i = 0; i < list.length; i++) {
            if (list[i] === obj[x]) {
                return true;
            } 
        }
    }
return false;
}
containsObject(data2,data);
Osama
  • 2,912
  • 1
  • 12
  • 15
  • but what if data2 contains more than one entry? – ItFreak Oct 04 '18 at 09:37
  • Rewriting the last comment as a snippet link for ease of understanding and in order to correct an error since you can't locate an object into an array without working a way to transform the objects. Hope it helps. https://jsfiddle.net/qpmy6acb/2/ – riot Oct 04 '18 at 12:21