1

I'm trying to see If a certain key, "channel" will exist in an array of objects. The array can have a single object or multiple. If there are multiple we need to check if they exist

The Example Below Has multiple

This is What I have currently, it works for a single object but not multiple

var obj = [
  {
    "Movies": {
      "7364234": "hsjd",
      "tom and jerry": "dsjdas",
      "mickey mouse": "kfjskdsad",
      "popeye the sailor man": "alkdsajd",
      "channel": "kasjdsjad"
    }
  },
  {
    "Movies": {
      "73642348": "hsjd",
      "terry and jon": "dsjdas",
      "mickey rat": "kfjskdsad",
      "popeye the sailor women": "alkdsajd",
      "channel": "kasjdsjad"
    }
  }
];

    if ( typeof obj[0]["Movies"]["channel"] !== 'undefined' ) {
        console.log('exists');
    } else {
        console.log('does not exist');
    }
WSB
  • 23
  • 2

5 Answers5

0

You can use optional chaining.

var obj = [{
    "Movies": {
      "7364234": "hsjd",
      "tom and jerry": "dsjdas",
      "mickey mouse": "kfjskdsad",
      "popeye the sailor man": "alkdsajd",
      "channel": "kasjdsjad"
    }
  },
  {
    "Movies": {
      "73642348": "hsjd",
      "terry and jon": "dsjdas",
      "mickey rat": "kfjskdsad",
      "popeye the sailor women": "alkdsajd",
      "channel": "kasjdsjad"
    }
  }
];
obj.forEach((item, index)=>{
  if (item?.["Movies"]?.["channel"]) {
    console.log('exists');
  } else {
    console.log('does not exist');
  }
});
kyun
  • 9,710
  • 9
  • 31
  • 66
0

If I understood correctly you can just loop through for the dynamic amount of objects within your array, you don't even need to change much of the code.

var obj = [
  {
    "Movies": {
      "7364234": "hsjd",
      "tom and jerry": "dsjdas",
      "mickey mouse": "kfjskdsad",
      "popeye the sailor man": "alkdsajd",
      "channel": "kasjdsjad"
    }
  },
  {
    "Movies": {
      "73642348": "hsjd",
      "terry and jon": "dsjdas",
      "mickey rat": "kfjskdsad",
      "popeye the sailor women": "alkdsajd",
      "channel": "kasjdsjad"
    }
  }
];

for (i = 0; i < obj.length; i++) {

  if ( typeof obj[i]["Movies"]["channel"] !== 'undefined' ) {
      console.log('exists');
  } else {
      console.log('does not exist');
  }
  }
mah111
  • 146
  • 8
0

So its just stringfy obj and see if key exist in json data or not No matter how deep it is

Please also check this question

const data = [
    {
        Movies: {
            7364234: "hsjd",
            "tom and jerry": "dsjdas",
            "mickey mouse": "kfjskdsad",
            "popeye the sailor man": "alkdsajd",
            channel: "kasjdsjad",
        },
    },
    {
        Movies: {
            73642348: "hsjd",
            "terry and jon": "dsjdas",
            "mickey rat": "kfjskdsad",
            "popeye the sailor women": "alkdsajd",
            channel: "kasjdsjad",
        },
    },
];

function exist(data, key) {
    return JSON.stringify(data).includes(`"${key}":`);
}
console.log(exist(data, "Movies"));
console.log(exist(data, "channel"));
console.log(exist(data, "mamad")); // Not exist
console.log(exist(data, "")); // Not exist

Mreza0100
  • 328
  • 1
  • 9
0

Below logic should check if at least one nested Movies object has channel property:

const channelCheck = data.some(item => item.Movies.hasOwnProperty('channel'));

If you need to check if all nested objects have the same key, use data.every instead of data.some.

grudinsky
  • 108
  • 6
0

Use array find method look for channel.

const find = arr => arr.find(({Movies: { channel }}) => channel != undefined);

var obj = [
  {
    "Movies": {
      "7364234": "hsjd",
      "tom and jerry": "dsjdas",
      "mickey mouse": "kfjskdsad",
      "popeye the sailor man": "alkdsajd",
      "channel": "kasjdsjad"
    }
  },
  {
    "Movies": {
      "73642348": "hsjd",
      "terry and jon": "dsjdas",
      "mickey rat": "kfjskdsad",
      "popeye the sailor women": "alkdsajd",
      "channel": "kasjdsjad"
    }
  }
];


if (find(obj)) {
  console.log('Found channel');
} else {
  console.log('Not Found');
}

if (find([])) {
  console.log('Found channel');
} else {
  console.log('Not Found');
}
Siva K V
  • 10,561
  • 2
  • 16
  • 29