How to compare two array of object and get common objects

后端 未结 3 427
太阳男子
太阳男子 2020-12-22 11:12

Hello guys I have two arrays

var elements = [{
        \"id\": \"id_1\",
        \"type\": \"input\",
        \"businesstype\": { \"type\": \"text\" }
    },         


        
3条回答
  •  情话喂你
    2020-12-22 11:46

    If your filterArray does not have further objects in its hierarchy, you can make do with this solution - see demo below:

    var elements=[{id:"id_1",type:"input",businesstype:{type:"text"}},{type:"label",id:"id_234"},{id:"id_16677",type:"div"},{id:"id_155",type:"input",businesstype:{type:"password"}}],filterArray=[{type:"input",businesstype:{type:"text"}},{type:"div"}];
    
    var result = elements.filter(function(e) {
      return filterArray.some(function(f) {
        return Object.keys(f).every(function(k) {
          return e.hasOwnProperty(k) && Object.keys(f[k]).every(function(n) {
            return e[k][n] == f[k][n];
          });
        });
      });
    });
    
    console.log(result);
    .as-console-wrapper {top: 0;max-height: 100%!important;}

提交回复
热议问题