Home > Web Front-end > Front-end Q&A > Does javascript have a map?

Does javascript have a map?

藏色散人
Release: 2023-01-07 11:47:09
Original
3775 people have browsed it

There is a map method in JavaScript, which is used to return a new array and process the elements in sequence according to the order of the original array elements; the map syntax is "array.map(function(currentValue,index,arr), thisValue) ".

Does javascript have a map?

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

Does javascript have map?

There is a map method in javascript.

JavaScript Array map() method definition and usage

The map() method returns a new array, and the elements in the array are the values ​​of the original array elements after calling the function.

The map() method processes elements sequentially in the order of the original array elements.

Note: map() will not detect empty arrays. map() does not change the original array.

Syntax

array.map(function(currentValue,index,arr), thisValue)
Copy after login

Parameter description

Parameter function(currentValue, index,arr) must: function, each element in the array will execute this Function

Parameter currentValue Mandatory: the value of the current element

index Optional: the index value of the current element

arr Optional: the array object to which the current element belongs

thisValue Optional: The object is used as the execution callback, passed to the function, and used as the value of "this".

If thisValue is omitted, or null or undefined is passed in, then the this of the callback function is the global object.

Return value: Returns a new array. The elements in the array are the values ​​of the original array elements after calling the function.

Example

Each element in the array is multiplied by the value specified in the input box, and a new array is returned:

var numbers = [65, 44, 12, 4];
function multiplyArrayElement(num) {
    return num * document.getElementById("multiplyWith").value;
}
function myFunction() {
    document.getElementById("demo").innerHTML = numbers.map(multiplyArrayElement);
}
Copy after login

Running effect:

GIF 2021-9-3 星期五 下午 4-18-02.gif

Recommended study: "javascript basic tutorial"

The above is the detailed content of Does javascript have a map?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template