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

筛选具有多个属性的多数组对象

  •  1
  • Jakegarbo  · 技术社区  · 8 年前

    我试图创建一个过滤器函数,它可以从给定的字符串键集中返回与我要查找的值匹配的数据结果。

    数组示例:

    let data = [
     { id:1 , data:{ name:"sample1",address:{ cat:"business" } } },
     { id:2 , data:{ name:"sample2",address:{ cat:"office"  }  } },
     { id:3 , data:{ name:"sample3",address:{ cat:"office"  } } },
     { id:4 , data:{ name:"sample4",address:{ cat:"office"  }  } }
     { id:5 , data:{ name:"sample5",address:{ cat:"home"  } } }
     { id:6 , data:{ name:"sample6",address:{ cat:"home"  }  } }
    ]
    
    
    
    function filter( collection , value ,key ){
      //code 
    }
    
    
    let result = filter( data , "business" , [ "data","address","cat" ] )
    
    console.log(result)
    

    预期结果为

    {id:1,数据:{name:“sample1”,地址:{cat:“business”}}},

    3 回复  |  直到 8 年前
        1
  •  4
  •   Eddie    8 年前

    你可以用 filter 搜索数据。使用 reduce 构造密钥。

    注: 滤波器 返回匹配元素的数组。如果您只喜欢第一场比赛,可以使用 find

    let data = [
      { id:1 , data:{ name:"sample1",address:{ cat:"business" } } },
      { id:2 , data:{ name:"sample2",address:{ cat:"office"  }  } },
      { id:3 , data:{ name:"sample3",address:{ cat:"office"  } } },
      { id:4 , data:{ name:"sample4",address:{ cat:"office"  }  } },
      { id:5 , data:{ name:"sample5",address:{ cat:"home"  } } },
      { id:6 , data:{ name:"sample6",address:{ cat:"home"  }  } }
    ];
    
    
    function filter(collection, value, key) {
      return collection.filter(o => key.reduce((c, v) => c[v] || {}, o) === value);
    }
    
    let result = filter(data, "business", ["data", "address", "cat"])
    
    console.log(result)
        2
  •  1
  •   Karthikeyan    8 年前
    function filter( collection , value ,key ){
      const getNestedObjectValue = (nestedObject, propertyPath) => {
          return propertyPath.reduce((obj, key) =>
              (obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObject);
    
      };
      return collection.filter( item => getNestedObjectValue(item, key) === value);
    }
    

    如果匹配,则filter函数将返回匹配对象的数组;如果不匹配,则返回空数组。

    let result = filter( data , "business" , [ "data","address","cat" ] );
    
    console.log(result); // [{"id":1,"data":{"name":"sample1","address":{"cat":"business"}}}]
    
    let result2 = filter( data , "office" , [ "data","address","cat" ] );
    
    console.log(result2); //[{"id":2,"data":{"name":"sample2","address":{"cat":"office"}}},{"id":3,"data":{"name":"sample3","address":{"cat":"office"}}},{"id":4,"data":{"name":"sample4","address":{"cat":"office"}}}]
    
    let result3 = filter( data , "vacation" , [ "data","address","cat" ] );
    
    console.log(result2); // [] 
    
        3
  •  0
  •   hygull    8 年前

    你可以试试下面的代码。

    如果不满足您的问题,或者您希望在代码中添加更多功能,请发表评论。我会更新我的答案。

    function filter( collection , value ,key ){
         for(var obj of collection) {
               if(obj[key[0]][key[1]][key[2]] == value)
               { 
                   return obj
               }
         }
         return null;
    }