Home  >  Article  >  Web Front-end  >  How to find the length and sum of elements of an array in JavaScript

How to find the length and sum of elements of an array in JavaScript

青灯夜游
青灯夜游Original
2022-09-20 14:11:552018browse

In JavaScript, you can use the length attribute to get the length of the array, the syntax is "array object.length"; you can use the reduce() or reduceRight() function to find the sum of elements, the syntax is "arr.reduce(function f (pre,curr){return pre cur})" or "arr.reduceRight(function f(pre,curr){return pre cur})".

How to find the length and sum of elements of an array in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript to find the length of an array

In JavaScript, you can use the length property of the array object to get the length of the array.

Each array has a length attribute, which returns the maximum length of the array, that is, its value is equal to the maximum subscript value plus 1. Since the numeric subscript must be less than 2^32-1, the maximum value of the length attribute is equal to 2^32-1.

Example 1

The following code defines an empty array, and then assigns a value to the element with the index equal to 100, then the length attribute returns 101. Therefore, the length attribute cannot reflect the actual number of array elements.

var a = [];  //声明空数组
a[100] = 2;
console.log(a.length);  //返回101

Output:

101

The length property is readable and writable and is a dynamic property. The length attribute value is also automatically updated as the array elements change. At the same time, if the length attribute value is reset, it will also affect the elements of the array. The specific instructions are as follows:

If the length attribute is set to a value smaller than the current length value, the array will be truncated, and the new length will be All other element values ​​will be lost.

If the length property is set to a value larger than the current length value, then the empty array will be added to the end of the array, causing the array to grow to the newly specified length, and the read values ​​​​will be undefined.

Example 2

The following code demonstrates the impact of dynamic changes in the length attribute value on the array.

var a = [1,2,3];  //声明数组直接量
a.length = 5;  //增长数组长度
console.log(a[4]);  //返回undefined,说明该元素还没有被赋值
a.length = 2;  //缩短数组长度
console.log(a[2]);  //返回undefined,说明该元素的值已经丢失

Output:

undefined
undefined

JavaScript to find the sum of arrays

##Method 1: Use reduce()

reduce() Calculate the array elements into a value (from left to right).

var a = [1, 2, 3, 4, 5];
var b =a.reduce(function f(pre, curr){
	return pre + curr;
	});
console.log(b);

How to find the length and sum of elements of an array in JavaScript

Description: The

reduce() method can call the specified callback function for all elements in the array. The return value of this callback function is the cumulative result, and this return value is provided as a parameter the next time the callback function is called. The specific usage is as follows:

array.reduce(callbackfn[, initialVaule]);

Parameter description:

  • array: required parameter, an array object.

  • callbackfn: required parameter, a function that accepts up to four parameters. The recover() method calls the callbackfn function once for each element in the array.

  • initialVaule: Optional parameter, if initialVaule is specified, it will be used as the initial value to start accumulation. The first call to the callbackfn function supplies this value as a parameter instead of an array value.

The return value of the reduce() method is the cumulative result obtained by the last call to the callback function.

If the parameter initialVaule is provided, the reduce() method will call the callbackfn function once for each element in the array (in ascending index order); if the initialVaule is not provided, the reduce() method will call the callbackfn function once for each element in the array. The callbackfn function is called for each element starting with 2 elements.

The return value of the callback function is provided as the previousValue parameter the next time the callback function is called. The return value obtained from the last call to the callback function is the return value of the recude() method. This method does not call the callback function for missing elements in the array.

The syntax of the callback function is as follows:

function callbackfn(previousValue, currentVaule, currentIndex, array);

Description of callback function parameters:

  • PreviousValue: The value obtained by the last call to the callback function. If initialValue is provided to the reduce() method, the previousValue is initialValue when the function is first called.

  • currentValue: The value of the current element array.

  • currentIndex: The numeric index of the current array element.

  • array: Array object containing the element.

When the callback function is first called, the value provided as a parameter depends on whether the reduce() method has an initialValue parameter. If initialValue is provided to the recude() method, the previousValue parameter is the initialValue and the currentValue parameter is the value of the 1st element in the array.

Method 2: Use reduceRight()

reduceRight() to calculate the array elements into a value (from right to left).

var arr = [1, 2, 3, 4, 5, 5];
var b =arr.reduceRight(function f(pre, curr){
	return pre + curr;
	});
console.log(b);

How to find the length and sum of elements of an array in JavaScript

Description: The

reduceRight() method can call the specified callback function on all elements in the array from right to left. The return value of this callback function is the cumulative result, and this return value is provided as a parameter the next time the callback function is called. The specific usage is as follows:

array.reduceRight(callbackfn[, initialValue]);

该方法的语法和用法与 reduce() 方法大概相同,唯一不同的是,它是从数组右侧开始调用回调函数。如果提供了 initialValue,则 reduceRight() 方法会按降序索引顺序对数组中的每个元素调用一次 callbackfn 函数。如果未提供 initialValue,则 reduceRight() 方法会按降序索引顺序对每个元素(从倒数第 2 个元素开始)调用 callbackfn 函数。

【相关推荐:javascript视频教程web前端开发

The above is the detailed content of How to find the length and sum of elements of an array in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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