Compare two multidimensional arrays in javascript

后端 未结 4 778
故里飘歌
故里飘歌 2021-01-23 09:18

I have two arrays:

var array_old = [{id:\"5436\", title:\"I Like you boy\"}, {id:\"5437\", title:\"Hello how are you\"}];
var array_new = [{id:\"5436\", title:\"         


        
4条回答
  •  滥情空心
    2021-01-23 09:52

    I found a way myself, but I don't know if this is the best way to do it:

    var array_old = [{id: "5436",title: "I Like you boy"},{id: "5437",title: "Hello how are you"},{id: "5438",title: "Hello how are you2"}];
    var array_new = [{id: "5436",title: "I Like you boy"},{id: "1132",title: "I'm fine"}];
    
    $.each(array_old, function(id, array){
    
        found = 0;
    
        $.each(array_new, function(id2, array2) {
    
            if(array['id']==array2['id'])
            {
                found++;
            }
    
        });
    
        if(found==0)
        {
            alert(array['id']+' does not exist in array_new');
        }
    
    });
    

    http://jsfiddle.net/FAb3k/2/

提交回复
热议问题