Home >Web Front-end >Front-end Q&A >How to determine whether there is a value in an array in es6
Two methods: 1. Get the length of the array and determine whether the length is 0. The syntax is "arr.length==0". If it is 0, there is no value in the array. 2. Convert the array to a JSON string and determine whether the string is "[]". The syntax is "JSON.stringify(arr)=='[]'". If so, there is no value in the array.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
es6 Determine whether there is a value in the array, that is, whether the array is empty. Two judgment methods are introduced below.
Method 1: Use the length attribute
Use the length attribute to get the length of the array and determine whether the length of the array is 0
If it is 0, there is no value in the array
If it is not 0, there is a value in the array
let arr = [1]; if (arr.length == 0){ console.log("数组中没有值"); }else { console.log("数组中有值"); } console.log(arr);
Method 2: Use JSON.stringify()
JSON.stringify() method to convert a JavaScript object or value into a JSON string; at this time, you only need to determine the Just check whether the JSON string is "[]".
If yes, then there is no value in the array
If not, then there is a value in the array
let arr = []; let str=JSON.stringify(arr); if (str == '[]'){ console.log("数组中没有值"); }else { console.log("数组中有值"); } console.log(str);
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of How to determine whether there is a value in an array in es6. For more information, please follow other related articles on the PHP Chinese website!