0
{
 "myJSONArrayObject": [
 {
 "12": {}
 },
 {
 "22": {}
 }
 ]
} 

I have the above JSON Array object. How can I check if myJSONArrayObject has a particular key?

This approach is not working :

let myIntegerKey = 12;
if (myJSONArrayObject.hasOwnProperty(myIntegerKey))
 continue;

It seems to return false when it contains a key and true when it doesn't.

ctaleck
1,66511 silver badges20 bronze badges
asked Aug 27, 2019 at 13:15
1
  • If it's possible, maybe you can use regular objects, so [{ "id": 12, "value": {} }, { "id": 22, "value": {} }]. Way easier to search/transform than when every object inside the array has different keys. Commented Aug 27, 2019 at 13:24

4 Answers 4

3

myJSONArrayObject is an array. It doesn't have 12 as a property (Unless there are 12+ items in the array)

So, check if some of the objects in the array have myIntegerKey as a property

const exists = data.myJSONArrayObject.some(o => myIntegerKey in o)

or if myIntegerKey is always an own property

const exists = data.myJSONArrayObject.some(o => o.hasOwnProperty(myIntegerKey))

Here's a snippet:

const data={myJSONArrayObject:[{"12":{}},{"22":{}}]},
 myIntegerKey = 12,
 exists = data.myJSONArrayObject.some(o => myIntegerKey in o);
console.log(exists)

answered Aug 27, 2019 at 13:18

1 Comment

This is great! Is there a way to check not just the key, but a key:value pairing with this method?
0

"myJSONArrayObject" is an array so you have to check hasOwnProperty on each of it's elements:

let myIntegerKey = 12;
for (var obj in myJSONArrayObject) {
 console.log(obj.hasOwnProperty(myIntegerKey));
}
answered Aug 27, 2019 at 13:19

Comments

0

The most direct method to retrieve the object by a key is to use JavaScript bracket notation. Use the find method to iterate over the array, also.

const obj = {
 myJSONArrayObject: [{
 12: {},
 },
 {
 22: {},
 },
 ],
};
const myIntegerKey = 12;
const myObject = obj.myJSONArrayObject.find(item => item[myIntegerKey]);
console.log("exists", myObject !== undefined);

answered Aug 27, 2019 at 13:33

Comments

0

const obj = {
 myJSONArrayObject: [
 {
 12: {},
 },
 {
 22: {},
 },
 ],
};
const myIntegerKey = '12';
const isExist = obj.myJSONArrayObject.findIndex((f) => { return f[myIntegerKey]; }) > -1;
console.log(isExist); 

You can make it faster with every()

const obj = {
 myJSONArrayObject: [
 {
 22: {},
 },
 {
 12: {},
 },
 ],
};
const myIntegerKey = '12';
const isExist = !obj.myJSONArrayObject
 .every((f) => {
 return !f[myIntegerKey];
 });
console.log(isExist); 

Note: Here the key name (12: {},) doesn't rely on typeof myIntegerKey, 12 and '12' both will return true.

answered Aug 27, 2019 at 13:22

1 Comment

Misunderstood your comment, thought you meant the other way around, that it Does matter sometimes and using this code prevents it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.