【问题标题】:Check if array of objects contain certain key检查对象数组是否包含某个键
【发布时间】:2020-08-11 17:56:29
【问题描述】:

我需要确定某个键是否存在于对象数组中。

这是一个示例数组:

arrOfObj = [{
        "mainKey1": {
            "subKey1": {
                "innerKey1": {
                    "innerMostKey1": {
                        "key1": "value"
                    }
                }
            }
        }
    }, {
        "mainKey2": {
            "key2": "value"
        }
    }, {
        "mainKey3": {
            "subKey3": {
                "key3": "value"
            }
        }
    }
]

我试图这样做,但我得到了错误的输出:

const objKeys = Object.keys(arrOfObj)
console.log('objKeys = ' + JSON.stringify(arrOfObj))

输出是索引号:

objKeys = ["0", "1", "2"]

我想要一个像这样工作的函数:

var isKeyPresent = checkKeyPresenceInArray('mainKey3')

请注意,尽管我只需要检查对象中的最顶层 - 在上面的示例中,这些是主键(mainKey1 等)并且它们的内容是动态的(其他一些内部有深层嵌套的对象并且有些不是这样。

救命!

【问题讨论】:

  • 您需要找到单个对象的键,而不是完整的数组,使用某种数组方法遍历数组,然后为每个单独的元素获取键并针对您想要的键进行测试
  • 要提取数组中的所有键吗?
  • 你得到 0,1,2 因为你传递的是一个数组,而不是一个对象。

标签: javascript arrays node.js json object


【解决方案1】:

你可以试试array.some():

let checkKeyPresenceInArray = key => arrOfObj.some(obj => Object.keys(obj).includes(key));

let arrOfObj = [{
        "mainKey1": {
            "subKey1": {
                "innerKey1": {
                    "innerMostKey1": {
                        "key1": "value"
                    }
                }
            }
        }
    }, {
        "mainKey2": {
            "key2": "value"
        }
    }, {
        "mainKey3": {
            "subKey3": {
                "key3": "value"
            }
        }
    }
]

let checkKeyPresenceInArray = key => arrOfObj.some(obj => Object.keys(obj).includes(key));


var isKeyPresent = checkKeyPresenceInArray('mainKey3')

console.log(isKeyPresent);

【讨论】:

    【解决方案2】:

    您可以遍历数组,检查是否有任何对象具有您要查找的键,如果有则返回 true。如果您没有找到密钥,则for 循环将完成并返回 false。

    arrOfObj = [{
            "mainKey1": {
                "subKey1": {
                    "innerKey1": {
                        "innerMostKey1": {
                            "key1": "value"
                        }
                    }
                }
            }
        }, {
            "mainKey2": {
                "key2": "value"
            }
        }, {
            "mainKey3": {
                "subKey3": {
                    "key3": "value"
                }
            }
        }
    ]
    
    function arrayHasKey(arr, key) {
      for (const obj of arr) {
        if (key in obj) { return true; }
      }
      return false;
    }
    
    console.log(arrayHasKey(arrOfObj, "mainKey2"))
    console.log(arrayHasKey(arrOfObj, "mainKey10"))

    【讨论】:

      【解决方案3】:

      这会起作用,它返回布尔值:

      arrOfObj.hasOwnProperty('mainKey3');
      

      【讨论】:

      • 即使键存在也会返回 false,因为它检查的是属性,而不是对象。
      【解决方案4】:

      您可以像这样使用somehasOwnProperty

      let checkKeyPresenceInArray = (key) => arrOfObj.some((o) => o.hasOwnProperty(key));
      

      【讨论】:

        【解决方案5】:

        您必须使用 hasOwnProperty 方法来检查该数组中的对象中的密钥是否可用 -

        var c = 0;
        arrOfObj.forEach(e => {
          if(e.hasOwnProperty("mainKey1")){
               c++;
           }
        });
        if(c > 0 && c == arrOfObj.length){
            console.log("The key is found in all the objects in the array.");
        }
        else if(c == 0){
            console.log("The key is not found in any objects in the array");
        }
        else{
             console.log("The key is  found in some objects in the array");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-27
          • 2015-01-22
          • 2020-06-07
          相关资源
          最近更新 更多