【问题标题】:Javascript check if string exists in array of objectsJavascript检查字符串是否存在于对象数组中
【发布时间】:2019-07-06 16:25:52
【问题描述】:

我的程序中有两个数组,一个包含用作键的字符串列表,另一个包含也包含键的对象。

let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];

仅当 key(作为 className 属性)存在于 classKeys 中时,我才需要返回另一个包含 classesInList 等对象的数组

例如,在这种情况下,我需要根据 classesInList 检查 classKeys 并创建一个包含两个项目的数组:

[{key : "WIHUGDYVWJ", className : 'math'},{key : "likubqwd", className : 'robotics'}]

因为英语不在classKeys中,它从classesInList中被删除。 我该怎么办?

旁注:我使用的是 react native,所以我可以访问 es6 属性和函数。

在这个意义上我完全被困住了。无论这看起来多么简单,我都找不到答案。

【问题讨论】:

  • 可能使用filter。你都尝试了些什么?什么地方出了错?展示您的最佳尝试并解释您遇到的问题(错误、意外结果等)

标签: javascript arrays reactjs object ecmascript-6


【解决方案1】:

只需使用Array.prototype.filter 并将其与Array.prototype.includes 结合使用。您要做的是过滤classesInList 对象数组,并仅选择className 值存在于classKeys 数组中的对象。

let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'}];

let filteredClasses = classesInList.filter(cls => classKeys.includes(cls.className));

console.log(filteredClasses);

【讨论】:

    【解决方案2】:

    可以使用filter数组方法,检查元素的className是否在classKeys数组中:

    let classKeys = ["math", "robotics"];
    let classesInList = [
      { key: "WIHUGDYVWJ", className: "math" },
      { key: "qwljdhkuqwdnqwdk", className: "english" },
      { key: "likubqwd", className: "robotics" }
    ];
    
    let result = classesInList.filter(element => {
      return classKeys.indexOf(element.className) !== -1;
    });
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      使用filter 将过滤您的数组,使用some 获取某些值是否符合您的标准,最后使用includes 检查该值是否包含在您的classKeys 中。

      let classKeys = ['math', 'robotics'];
      let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];
      
      
      const result = classesInList
                     .filter(c => 
                        Object.values(c).some(val => 
                                              classKeys.includes(val)
                        )
                      );
      
      console.log(result);

      【讨论】:

        【解决方案4】:

        Array.prototype.filterArray.prototype.includes 使用解构:

        let classKeys = ['math', 'robotics'];
        let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'}];
        
        let filtered = classesInList.filter(({ className }) => classKeys.includes(className));
        
        console.log(filtered);

        【讨论】:

          【解决方案5】:

          您可以在 ES6 中使用filterincludes 和对象解构来做到这一点:

          const classKeys = ['math', 'robotics'];
          const classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];
          
          const result = classesInList.filter(({ className }) => classKeys.includes(className));
          
          console.log(result);

          【讨论】:

            猜你喜欢
            • 2013-05-20
            • 2022-01-03
            • 2020-07-06
            • 1970-01-01
            • 2014-09-10
            • 1970-01-01
            • 2021-12-27
            • 1970-01-01
            相关资源
            最近更新 更多