【问题标题】:Java Script filter nested object properties by valueJavascript按值过滤嵌套对象属性
【发布时间】:2021-09-27 00:56:00
【问题描述】:

我需要按属性值过滤嵌套对象。我知道以前有人问过类似的问题,但是对于将值存储在数组中的情况,我找不到解决方案。

在提供的代码示例中,我需要根据标签过滤对象。我想在标签数组中获取包含“a”和“b”的对象。

const input1 = {
    "0":{
        "id":"01",
        "name":"item_01",
        "tags":["a","b"],
    },
    "1":{
        "id":"02",
        "name":"item_02",
        "tags":["a","c","d"],
    },
    "2":{
        "id":"03",
        "name":"item_03",
        "tags":["a","b","f"],
    }
}
 
function search(input, key) {
   return Object.values(input).filter(({ tags }) => tags === key);
}

console.log(search(input1, "a"));

作为输出,我想收到休耕:

{
    "0":{
        "id":"01",
        "name":"item_01",
        "tags":["a","b"],
    },
    "2":{
        "id":"03",
        "name":"item_03",
        "tags":["a","b","f"],
    }
}

提前非常感谢!

【问题讨论】:

标签: javascript arrays filter nested-object


【解决方案1】:
function search(input, key) {
  Object.values(input).filter(x=>x.tags.includes(key))
}

【讨论】:

    【解决方案2】:

    由于您想保留对象结构,您应该使用Object.entries 而不是Object.values 并恢复为对象类型使用Object.fromEntries

    Object.fromEntries(Object.entries(input).filter(...))
    

    要使其适用于多个键,请将everyincludes 结合使用作为谓词:

    keys.every(key => tags.includes(key))
    

    const input1 = {
        "0":{
            "id":"01",
            "name":"item_01",
            "tags":["a","b"],
        },
        "1":{
            "id":"02",
            "name":"item_02",
            "tags":["a","c","d"],
        },
        "2":{
            "id":"03",
            "name":"item_03",
            "tags":["a","b","f"],
        }
    }
     
    function search(input, keys) {
       return Object.fromEntries(
       Object.entries(input).filter(([, { tags }]) => keys.every(key => tags.includes(key)))
       )
       
    }
    
    console.log(search(input1, ["a", "b"]));

    【讨论】:

    • 谢谢!这正是我想要的!
    【解决方案3】:

    您可以使用Object.entries[key, value] 对作为数组的数组,然后您可以使用filter 过滤掉不包含key 数组中的元素的元素。最后,您可以使用reduce 生成单个值,即作为最终结果的对象

    const input1 = {
      "0": {
        id: "01",
        name: "item_01",
        tags: ["a", "b"],
      },
      "1": {
        id: "02",
        name: "item_02",
        tags: ["a", "c", "d"],
      },
      "2": {
        id: "03",
        name: "item_03",
        tags: ["a", "b", "f"],
      },
    };
    
    function search(input, key) {
      return Object.entries(input)
        .filter(([, v]) => key.every((ks) => v.tags.includes(ks)))
        .reduce((acc, [k, v]) => {
          acc[k] = v;
          return acc;
        }, {});
    }
    
    console.log(search(input1, ["a", "b"]));

    【讨论】:

      【解决方案4】:
         function search(input, tagsToFind) {
        return Object.values(input).filter(inputItem => {
          tagsToFind = Array.isArray(tagsToFind) ? tagsToFind : [tagsToFind];
          let tagFound = false;
          for (const key in tagsToFind) {
            if (Object.prototype.hasOwnProperty.call(tagsToFind, key)) {
              const element = tagsToFind[key];
              if (inputItem.tags.indexOf(element) === -1) {
                tagFound = false;
                break;
              } else {
                tagFound = true;
              }
            }
          }
      
          return tagFound;
        })
      
        // ({ tags }) => tags === key);
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2021-05-09
        • 1970-01-01
        • 2016-02-27
        • 1970-01-01
        • 2022-01-24
        • 2018-07-09
        • 2022-08-18
        • 1970-01-01
        • 2021-01-15
        相关资源
        最近更新 更多