0

This is my object data:

[{id:1, name:"Cat", category:{id:2, name:"Animals"}}]

I want to check if it contains the key category.

This is my approach:

if (data.hasOwnProperty("category")) {
console.log("data contains category");
}else {
  console.log("data does not contain category");
}

The output is:

data does not contain category

Should be the opposite...

peace_love
  • 6,229
  • 11
  • 69
  • 157
  • You have an array, not an object. If you should have an object, you should be looking at the source of `data`. – Devon Bessemer Feb 25 '19 at 16:52
  • @Devon. It must be an object, because if I `alert(data.toString());` this is the ouput: `[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]` – peace_love Feb 25 '19 at 16:56
  • 2
    It's got `[ ]` around an object; it's an array with one object in it. – Pointy Feb 25 '19 at 16:56
  • @Jarla, that's probably not the easiest way to see the data, use your console, not alerts. Beyond that, you have an array of multiple objects from the looks of that, not a single object. I think the problem is you're expecting a single object when you have an array of objects. Whether the producer is wrong or whether the consumer's expectation is wrong is up to you to figure out. – Devon Bessemer Feb 25 '19 at 17:00
  • @Devon Thank you, I am always confused with this question...is it an object or is it an array – peace_love Feb 25 '19 at 17:47

4 Answers4

2

You need to iterate your array. So you can put the code inside a forEach

let k = [{
  id: 1,
  name: "Cat",
  category: {
    id: 2,
    name: "Animals"
  }
}]

k.forEach(function(data) {
  if (data.hasOwnProperty("category")) {
    console.log("data contains category");
  } else {
    console.log("data does not contain category");
  }
})

If you dont prefer to iterate you need to pass the index , since data is an array of object.

let data = [{
  id: 1,
  name: "Cat",
  category: {
    id: 2,
    name: "Animals"
  }
}]


if (data[0].hasOwnProperty("category")) {
  console.log("data contains category");
} else {
  console.log("data does not contain category");
}
brk
  • 48,835
  • 10
  • 56
  • 78
1

You can also use some to check if any object in your array matches a certain condition:

var data = [{
  id: 1,
  name: "Cat",
  category: {
    id: 2,
    name: "Animals"
  }
}];

var hasCategory = data.some(k => k.hasOwnProperty('category'));

if (hasCategory) {
  console.log("data contains category");
} else {
  console.log("data does not contain category");
}
slider
  • 12,810
  • 1
  • 26
  • 42
1

You need to loop through array elements. and you can destructure assignment

let arr = [{id:1, name:"Cat", category:{id:2, name:"Animals"}}]

arr.forEach(({category})=>{
  if (category !== undefined) {
   console.log("data contains category");
  }else {
   console.log("data does not contain category");
  }
})
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 1
    Just a side note, if `category` is defined to `null`, `0`, or any other falsy value, your current code will display: _data does not contain category_. Maybe you can improve it changing the check condition to `category !== undefined`. – Shidersz Feb 25 '19 at 17:02
  • @CodeManiac You are welcome. I hope your down-vote gets reverted, was not my intention you get down-voted, sorry for that. – Shidersz Feb 25 '19 at 17:14
  • @Shidersz no problem mate :) down vote do not bother me. only motive is learning. – Code Maniac Feb 25 '19 at 17:15
0

That's an array with only one index, so you can do this using the operator in:

"category" in data[0] // That checks if the property "category" exists.

If you have more than one index, you should loop that array.

The operator in also checks for inherited properties.

let data = [{id:1, name:"Cat", category:{id:2, name:"Animals"}}];
data.forEach((o, i) => console.log("Index:", i, " - Has category?:", "category" in o));
Ele
  • 33,468
  • 7
  • 37
  • 75