最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if multiple keys exists in JSON object - Stack Overflow

programmeradmin2浏览0评论

I am trying to validate the request object to check if specific keys exist in the object or not. I've tried lodash's has() function, but it seems that _.has() checks nested JSON. JavaScript's .hasOwnProperty() takes one key at a time. Is it possible to check an array of keys within a plain JSON object?

The object I am trying to check is:

{
    "name": "[email protected]",
    "oldPassword": "1234",
    "newPassword": "12345"
}

I am trying to validate the request object to check if specific keys exist in the object or not. I've tried lodash's has() function, but it seems that _.has() checks nested JSON. JavaScript's .hasOwnProperty() takes one key at a time. Is it possible to check an array of keys within a plain JSON object?

The object I am trying to check is:

{
    "name": "[email protected]",
    "oldPassword": "1234",
    "newPassword": "12345"
}
Share Improve this question edited Oct 9, 2022 at 7:00 asynts 2,4232 gold badges26 silver badges37 bronze badges asked Feb 26, 2019 at 9:09 ShashankShashank 4072 gold badges8 silver badges17 bronze badges 3
  • 2 Why not check like obj.name && obj.oldPassword && obj.newPassword ? If you want to check for a fixed structure, you need to have some sort of types like Typscript provides for example. – David Joos Commented Feb 26, 2019 at 9:11
  • 1 @DavidJoos It will be a bit tedious in case the object gets multiple keys. – Shashank Commented Feb 26, 2019 at 9:51
  • 1 sure, but especially in this case i wouldn't hesitate to use it like this. – David Joos Commented Feb 26, 2019 at 9:54
Add a comment  | 

4 Answers 4

Reset to default 29

Simply use Object.keys and every

const neededKeys = ['oldPassword', 'name', 'newPassword'];

const obj = {
    "name": "[email protected]",
    "oldPassword": "1234",
    "newPassword": "12345"
}

console.log(neededKeys.every(key => Object.keys(obj).includes(key)));

You can use in operator to check if keys exist in object or not. It is quite faster than Object.keys as its complexity is O(1) as compared to Object.keys with complexity of O(n)

Example:

const neededKeys = ['oldPassword', 'name', 'newPassword'];

const obj = {
    "name": "[email protected]",
    "oldPassword": "1234",
    "newPassword": "12345"
}

console.log(neededKeys.every(key => key in obj));

You can use .includes method of an array and Object.keys will give you an array of all the keys. You can compare this with an array of keys from which you want to check using a loop

var a = {
  "name": "[email protected]",
  "oldPassword": "1234",
  "newPassword": "12345"
};
var key = ["name", "oldPassword", "newPassword"];
Object.keys(a).forEach(e => key.includes(e) ? console.log(e + " found") : console.log(e + " not found"))

myobj =  {
    "name": "[email protected]",
    "oldPassword": "1234",
    "newPassword": "12345"
}
Print (myobj.keys() >= {"name","oldPassword","newPassword"})  

-- this will return True

Print (myobj.keys() >= {"name1","id","firstname"})  

-- this will return false

发布评论

评论列表(0)

  1. 暂无评论