简体   繁体   English

使用过滤器查找匹配的键和值

[英]Find matching keys and values using filter

I need to check if a key value pair exists in json. 我需要检查JSON中是否存在键值对。

filter: {department_Id: 20}
tk: {department_Id: 22, timekeeper_Name: "Tesvtda", office_Id: 2, position_Id: 1}

I can check if the filter key exists in the object: 我可以检查对象中是否存在过滤器键:

return tk.hasOwnProperty(Object.keys(filter)) ;

But, how can I now check the value? 但是,现在如何检查该值?

Full function: 功能齐全:

filteredTimekeeperList: function() {
  return this.timekeepersList.filter(tk => {
    return this.filters.forEach(filter => {
      console.log(filter, tk);
      console.log(tk.hasOwnProperty(Object.keys(filter)));
      return tk.hasOwnProperty(Object.keys(filter)) ; // True, now I need to return if both are matches
    });
  });
}

Example: https://codesandbox.io/s/y3xqo51wqv 示例: https//codesandbox.io/s/y3xqo51wqv

Input:
 timekeepersList: [
        {
          department_Id: 20,
          timekeeper_Name: "Test",
          office_Id: 1,
          position_Id: 1
        },
        {
          department_Id: 20,
          timekeeper_Name: "Test",
          office_Id: 1,
          position_Id: 1
        },
        {
          department_Id: 21,
          timekeeper_Name: "Test",
          office_Id: 1,
          position_Id: 1
        }
      ],
      filters: [
        {
          department_Id: 21
        }
      ]

Output: 
        {
          department_Id: 21,
          timekeeper_Name: "Test",
          office_Id: 1,
          position_Id: 1
        }

You can do it like this: 您可以这样做:

The idea is that you want to filter the timeKeepersList if the value of a given key matches that of the filters array objects. 想法是,如果给定键的值与filters数组对象的值匹配, timeKeepersList过滤timeKeepersList This is where .some() and .every() will come very much in handy. 这就是.some().every()派上用场的地方。

Using .every() will make sure that every key in every object in the filters array matches that of the given timekeeper. 使用.every()将确保filters数组中每个对象中的每个键都与给定的计时器保持一致。 Using .some() will return a given timekeeper if any key from any object in the filters array matches that of the timekeeper. 如果filters数组中任何对象的任何键与计时器的键匹配,则使用.some()将返回给定的计时器。

You can also obviously mix and match them, depending on your case. 您还可以根据情况将它们混合并匹配。 If you want to return a timekeeper if any object matches all keys, or if all objects match any key. 如果要返回一个计时器,则是任何对象都与所有键匹配,或者所有对象都与任何键匹配。 Those cases would be .some() -> .every() and .every() -> .some() , respectively. 这些情况分别是.some() -> .every().every() -> .some()

 var timekeepersList = [ { department_Id: 20, timekeeper_Name: "Test", office_Id: 1, position_Id: 1 }, { department_Id: 21, timekeeper_Name: "Tesd332t", office_Id: 1, position_Id: 1 }, { department_Id: 20, timekeeper_Name: "Tesct123", office_Id: 2, position_Id: 2 }, { department_Id: 21, timekeeper_Name: "Tesat1ffg23", office_Id: 2, position_Id: 2 }, { department_Id: 22, timekeeper_Name: "Tesvtda", office_Id: 2, position_Id: 1 } ]; var filters = [{department_Id: 20}]; var filteredData = timekeepersList.filter(tk => { // change .every to .some if you want partial matches return filters.every(function (filter) { // change .every to .some if you want partial matches return Object.keys(filter).every(function (key) { return tk[key] === filter[key]; }); }); }); console.log(filteredData); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM