Details on using JS array methods some, every and find

Details on using JS array methods some, every and find

1. some

some() method tests whether there is at least one element in the array that passes the test of the provided function. It returns a Boolean value.

In short: it checks each item in the array, and as long as one item passes, it is true .

  • It will only return true or false
  • It will test each item in the array. Never perform if else operations in some .
  • Don't write true or false after return . return is followed by your condition.

Recently, I encountered a requirement when working on a backend management system: a Dialog pops up. As long as there is a value in input of the Dialog , it will be OK; otherwise, it is prompted that at least one value must be present.

The data structure is as follows, using some

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, res is true , so we can proceed to the next step when res is true .

if (res) {
    console.log("array has value");
} else {
    console.log("Enter at least one value");
}

2. every

every can be used in the same way as some . From MDN: every() method tests whether all elements in an array pass a test of a specified function. It returns a Boolean value.

In short: it checks each item in the array, and if any item fails it is false .
Same precautions as for some . If each input box must have a value,

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, res2 will be false .

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 MDN , the find() method returns the value of the first element in an array that satisfies the provided test function. Otherwise returns undefined
Note: find() is different from the above two. It returns the value, and it is the first value that meets the conditions.

let arr3 = [
    { value: "" },
    { value: "" },
    { value: "" },
    { value: "" },
    { value: "apple" },
]
var res3 = arr3.find(item => {
    return item.value !== ""
})
console.log(res3);

You can determine valu in item based on whether the return value of find is undefined .

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, every and find . For more information about the usage of JS array methods some , every and find , please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The usage and advantages and disadvantages of for, for in, for of, map, forEach in JS array traversal
  • JavaScript's three methods of traversing arrays: map, forEach and filter
  • Javascript array loop traversal forEach detailed explanation
  • Learn the foreach method and some method in javascript array

<<:  MySQL complete collapse query regular matching detailed explanation

>>:  How to use map to allow multiple domain names to cross domains in Nginx

Recommend

JavaScript implementation of magnifying glass details

Table of contents 1. Rendering 2. Implementation ...

Detailed analysis and usage of tcpdump command under Linux

Introduction To put it simply, tcpdump is a packe...

Brief introduction and usage of Table and div

Web front end 1 Student ID Name gender age 01 Zha...

Detailed graphic tutorial on how to enable remote secure access with Docker

1. Edit the docker.service file vi /usr/lib/syste...

The top fixed div can be set to a semi-transparent effect

Copy code The code is as follows: <!DOCTYPE ht...

MySQL uninstall and install graphic tutorial under Linux

This is my first time writing a blog. I have been...

Introduction to new features of ECMAscript

Table of contents 1. Default values ​​for functio...

Steps to modify the MySQL database data file path under Linux

After installing the MySQL database using the rpm...

jQuery achieves the effect of advertisement scrolling up and down

This article shares the specific code of jQuery t...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

Use pure CSS to achieve scroll shadow effect

To get straight to the point, there is a very com...

JavaScript to achieve a simple page countdown

This article example shares the specific code of ...

How to use vw+rem for mobile layout

Are you still using rem flexible layout? Does it ...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...