How does es6 determine whether there is a value in an array?
This article mainly introduces how es6 judges whether there are values in the array, the content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe everyone will have some gains after reading this es6 article on how to judge whether there are values in the array. Let's take a look at it together.
Two methods: 1, get the array length, determine whether the length is 0, syntax "arr.length==0", if 0 then there is no value in the array. 2. Convert the array to a JSON string, determine whether the string is "[]", syntax "JSON.stringify(arr)=='[]'", if so, there is no value in the array.
Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
ES6 determines whether there is a value in the array, that is, whether the array is empty. Here are two ways to judge.
Method 1: Use the length attribute
Use the length attribute to get the array length and determine whether the array length is 0.
If 0, there are no values in the array.
If it is not 0, there is a value in the array.
let arr = [1];if (arr.length == 0){ console.log("No values in array");}else { console.log("array has values");}console.log(arr);
Method 2: Use JSON.stringify()
The JSON.stringify() method converts a JavaScript object or value to a JSON string; in this case, you simply need to determine whether the JSON string is "[]."
If so, there are no values in the array.
If not, there are values in the array.
let arr = [];let str=JSON.stringify(arr);if (str == '[]'){ console.log("No values in array");}else { console.log("array has values");}console.log(str);
About "es6 how to determine whether there is a value in the array" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "how es6 judges whether there is a value in the array." If you still want to learn more knowledge, please pay attention to the industry information channel.