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

Book page turning effects made with CSS3

Result:Implementation code: html <!-- Please h...

CSS3 sample code to achieve element arc motion

How to use CSS to control the arc movement of ele...

MySql multi-condition query statement with OR keyword

The previous article introduced the MySql multi-c...

MySQL partitioning practice through Navicat

MySQL partitioning is helpful for managing very l...

How to connect idea to docker to achieve one-click deployment

1. Modify the docker configuration file and open ...

Solution to IDEA not being able to connect to MySQL port number occupation

I can log in to MYSQL normally under the command ...

Building .NET Core 2.0 + Nginx + Supervisor environment under Centos7 system

1. Introduction to Linux .NET Core Microsoft has ...

MySql 5.7.21 free installation version configuration method under win10

1. Unzip to the location where you want to instal...

The use of setState in React and the use of synchronous and asynchronous

In react, if you modify the state directly using ...

More elegant processing of dates in JavaScript based on Day.js

Table of contents Why use day.js Moment.js Day.js...

Detailed explanation of the use of title tags and paragraph tags in XHTML

XHTML Headings Overview When we write Word docume...

Detailed explanation of the role of explain in MySQL

1. MYSQL index Index: A data structure that helps...

In-depth understanding of Mysql logical architecture

MySQL is now the database used by most companies ...

A simple way to restart QT application in embedded Linux (based on QT4.8 qws)

Application software generally has such business ...

iframe multi-layer nesting, unlimited nesting, highly adaptive solution

There are three pages A, B, and C. Page A contains...