JS determine whether the object is empty HTTPS: // www.cnblogs.com/mountain-mist/articles/1600995.html HTTP: // www.cftea.com/c/2007/04/YK4PY29JW82CZOVY.asp
How to determine whether a JS array is empty if it contains a value? The following methods can not determine whether the object is 0, null, undefined 1 .js determine whether the array is empty? ARR the let = []; IF (arr.length == 0 ) { the console.log ( "empty array" ) } the else { the console.log ( "array is not empty" ) } 2 . determines whether the array contains a JS value? Method a: arr.indexOf () IF (! Arr.indexOf (2) = -1 ) { the console.log ( "array containing 2" ) } the else { the console.log ( "array excluding 2" ) } Method two: for determining if binding loop for (the let I = 0; I <arr.length; I ++ ) { IF (ARR [I] === 2 ) { the console.log ( "array containing 2 ' ) } } Method three: arr.find (the callback) arr.find (value => { IF (value === 2 ) { the console.log ( "array containing 2 ' ) } }) method IV: arr.includes () returns true array contains a certain value, no returns false. ES7 new methods. ARR the let = [1,2,3,4 ]; IF (arr.includes (2 )) { the console.log ( "array with a 2" ); } the else { the console.log ( "no array 2" ); }