How to determine whether there is a value in an array in es6

青灯夜游
Release: 2022-05-19 16:46:08
Original
1367 people have browsed it

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.

How to determine whether there is a value in an array in es6

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);
Copy after login

How to determine whether there is a value in an array in es6

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);
Copy after login

How to determine whether there is a value in an array in es6

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!