Find OR and AND of array elements using JavaScript

WBOY
Release: 2023-08-25 17:25:13
forward
794 people have browsed it

使用 JavaScript 查找数组元素的 OR 和 AND

In JavaScript, we can use the "&&" operator to perform an AND operation between two elements, and the "||" operator to perform an OR operation between two elements. .

Here we will learn to perform OR and AND operations between each element of an array.

Use for loop

We can use the for loop to iterate the array elements and perform OR operation on the array elements and the result elements.

grammar

Users can use the for loop to perform OR or AND operations on each element according to the following syntax.

for () { result = result || array[i]; }
Copy after login

In the above syntax, "result" is the result value of the OR operation of the first i-1 elements.

Example 1

In the example below, we create an array of numbers. After that, we call the PerformOR() function by passing the array and its length as parameters.

We define the "result" variable and initialize it to zero. After that, we use a for loop to iterate over the array elements. In the for loop, we perform an OR operation on the array elements and the value of the "result" variable.

In the output, the user can observe the result value of the OR operation on all array elements.

  
Copy after login

Example 2

In the example below, we perform an AND operation on each array element. First, we initialize the result variable with the first array element. After that, we use a for loop to iterate from the second array element and AND the array element with the value of the result variable.

  

Using the for loop to perform AND operation for all array elements in JavaScript

Copy after login

Using While Loop

While loops are also useful for iterating over arrays of numbers like a for loop. When iterating over array elements, we can perform AND or OR operations on individual array elements with the result variable.

grammar

Users can use the while loop to find the OR and AND of array elements according to the following syntax.

while (n--) { result = result && array[n]; }
Copy after login

In the above syntax, the result variable contains the result value of the OR and AND operations of the array elements.

Example 3

In the following example, we use a while loop and the "&&" operator to find the AND of all array elements. We initialize the result variable with the last element of the array. After that, we decrement the value of n by 1 on each iteration and AND the array elements with the value of the result variable.

  

Using the while loop to perform AND operation for all array elements in JavaScript

Copy after login

Use Array.reduce() method

array.reduce() Reduces an array to a single result value. Here we can reduce the array so that we can get the AND or OR of all the array elements.

grammar

Users can use the array.reduce() method according to the following syntax to find the AND or OR of all array elements.

array.reduce((res, current) => res || current, 0);
Copy after login

Example 4

In the following example, the performOR() function returns the OR of all array elements. In the function, we have used the reduce() method to reduce the array and OR all the elements.

In the output, the user can observe the OR result values of all elements.

Using thearray.reduce()method to perform OR operation for all array elements in JavaScript

Copy after login

Example 5

In this example, we use the array.reduce() method to perform an AND operation on all array elements. We pass the callback function as the first parameter of the array.reduce() method and the first array element as the second parameter, representing the initial value of the "res" variable. The 'res' variable contains the final value of the AND operation of the array elements.

  

Using the array.reduce() method to perform AND operation for all array elements in JavaScript

Copy after login

Conclusion

The user learned to find the AND or OR of array elements. In the first method we used for loop and in the second method we used while loop. In the third approach, we used the array.reduce() method, which is more readable code.

The above is the detailed content of Find OR and AND of array elements using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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 admin@php.cn
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!