1. some In short: it checks each item in the array, and as long as one item passes, it is
Recently, I encountered a requirement when working on a backend management system: a The data structure is as follows, using let arr = [ { value: "apple" }, { value: "" }, { value: "banana" }, { value: "orange" }, { value: "" }, ] let res = arr.some(item=>{ return item.value !== "" }) console.log(res); Here, as long as there is a value, if (res) { console.log("array has value"); } else { console.log("Enter at least one value"); } 2. every In short: it checks each item in the array, and if any item fails it is let arr2 = [ { value: "apple" }, { value: "" }, { value: "banana" }, { value: "orange" }, { value: "er" }, ] var res2 = arr2.every(item => { return item.value !== "" }) console.log(res2); Here, as long as there is no value for one item, if (!res2) { //If res2 is true, then go to else; if it is false, then go to if console.log("The input box has an empty value"); } else { console.log("The input box has no empty value"); console.log("Proceed to the next step"); } 3. find From let arr3 = [ { value: "" }, { value: "" }, { value: "" }, { value: "" }, { value: "apple" }, ] var res3 = arr3.find(item => { return item.value !== "" }) console.log(res3); You can determine if (res3) { //res3 has a value, proceed to the next step here. console.log("There is at least one value in the array"); } else { //res3 is undefined console.log("The array is empty!"); } This is the end of this article about the details of using JS array methods some, You may also be interested in:
|
<<: MySQL complete collapse query regular matching detailed explanation
>>: How to use map to allow multiple domain names to cross domains in Nginx
Table of contents 1. Rendering 2. Implementation ...
Introduction To put it simply, tcpdump is a packe...
Web front end 1 Student ID Name gender age 01 Zha...
1. Edit the docker.service file vi /usr/lib/syste...
Copy code The code is as follows: <!DOCTYPE ht...
This is my first time writing a blog. I have been...
Table of contents 1. Default values for functio...
After installing the MySQL database using the rpm...
This article shares the specific code of jQuery t...
When we edit a layout, we usually use horizontal ...
The default remote repository of Nexus is https:/...
To get straight to the point, there is a very com...
This article example shares the specific code of ...
Are you still using rem flexible layout? Does it ...
In fact, it is very simple to encapsulate axios i...