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...

5
  • You have an array, not an object. If you should have an object, you should be looking at the source of data. Commented Feb 25, 2019 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] Commented Feb 25, 2019 at 16:56
  • 2
    It's got [ ] around an object; it's an array with one object in it. Commented Feb 25, 2019 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. Commented Feb 25, 2019 at 17:00
  • @Devon Thank you, I am always confused with this question...is it an object or is it an array Commented Feb 25, 2019 at 17:47

4 Answers 4

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");
}

Sign up to request clarification or add additional context in comments.

Comments

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");
}

Comments

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");
  }
})

3 Comments

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.
@CodeManiac You are welcome. I hope your down-vote gets reverted, was not my intention you get down-voted, sorry for that.
@Shidersz no problem mate :) down vote do not bother me. only motive is learning.
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));

Comments

Your Answer

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.