Emiliano Maccaferri Emiliano Maccaferri - 1 year ago 77
Javascript Question

Check if object doesn't contain a value not working (JS)

I'm having an hard time solving this problem.

For some reason .hasOwnProperty() is not working.

What I want to do is to create a new object (inside another array) when a value (inside an array) doesn't exist in the array I'm currently looping.

I don't know why, but the first condition gets looped everytime, instead of being looped only when the condition is satisfied.

P.S I'm looping into JSON. Example of JSON I'm parsing

var keys = [



var resArray = stats.playerstats.stats;
var statsArray = [];

for (var i = 0; i < keys.length; i++) {
for (var j = 0; j < resArray.length; j++) {
if (!resArray[j]["name"].hasOwnProperty(keys[i])) {
resArray[j]["name"] = keys[i];
resArray[j]["value"] = "None";
"name": resArray[j]["name"],
"value": resArray[j]["value"]
if (resArray[j]["name"] === (keys[i])) {
"name": resArray[j]["name"],
"value": resArray[j]["value"]


Feel free to ask for clarifying.

Answer Source

if (!resArray[j]["name"].hasOwnProperty(keys[i])) {

Obviously this statement is wrong. your resArray[j]["name"] is string, not object. In usual you want to check if one object has some enumable property to use hasOwnProperty method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download