Filter array of objects with another array of objects

前端 未结 9 1718
小蘑菇
小蘑菇 2020-11-27 05:18

This question is similar to this one Jquery filter array of object with loop but this time I need to do the filter with an array of objects.

Exemple:

I have

相关标签:
9条回答
  • 2020-11-27 06:12

    You can use jquery map, this return one array of matches:

    var _filter=function(arr_data,arr_filter){
        return $.map( arr_data, function( n ) {
            for(var f in arr_filter){
                if(arr_filter[f].userid == n.userid && arr_filter[f].projectid == n.projectid){
                    return n;
                }
            }
        });
    }
    var resp = _filter(myArray,myFilter);
    console.log(resp);
    
    0 讨论(0)
  • 2020-11-27 06:16

    based on @Renato his answer, but shorter:

    const myArray = [{ userid: "100", projectid: "10", rowid: "0" }, ...];
    const myFilter = [{ userid: "101", projectid: "11" }, ...];
    
    const myArrayFiltered = myArray.filter(array => myFilter.some(filter => filter.userid === array.userid && filter.projectid === array.projectid));
    
    0 讨论(0)
  • 2020-11-27 06:21
    var filtered = [];
    
    for(var arr in myArray){
       for(var filter in myFilter){
           if(myArray[arr].userid == myFilter[filter].userid && myArray[arr].projectid == myFilter[filter].projectid){
              filtered.push(myArray[arr].userid);
             }
       }
    }
    console.log(filtered);
    
    0 讨论(0)
提交回复
热议问题