JavaScript Determines whether an array has a simple instance of key

  • 2021-07-07 06:29:54
  • OfStack

The composite array associative array in JS is equivalent to the object. To judge whether an key exists in the array (or whether the object contains a certain attribute), ary [key] = = undefined cannot be used, because ary = {key: undefined} may exist; The correct method should be:

ary. hasOwnProperty (key); Or obj. hasOwnProperty (key);

In addition, when using key-value pair to perform loop on composite arrays or objects, you should use:

for(var key in ary) { document.write(key+" : "+ary[key]); }


Related articles: