Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
how to use .map function in jquery?


What I have tried:

how to use .map function in jquery?
Posted
Updated 4 days ago
Comments
Richard Deeming 14-Aug-19 5:58am
   
This site does not charge you for each word you type. Help us to help you by providing a proper description of your question.

If you're just going to type the same terse sentence three times, then type it into Google's search box, not the "ask a question" box.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
how to use .map function in jquery?
The map function enables you to perform a function on each element of an array. So this,

var numbers = [ 1, 2, 3, 4, 5 ];
numbers = numbers.map(number => number + 10);
console.log(numbers);

// expected to be
// 11, 12, 13, 14, 15
It is similar to,
var numbers = [ 1, 2, 3, 4, 5 ];
for (var number in numbers) {
    numbers[number] = numbers[number] + 10;
}
console.log(numbers);
A little bit bizarre, but that is what it does. It lets you perform an action on each element, without having to worry about iterators, incremental variables, etc. Also it looks cleaner.

Check more here on MDN, Array.prototype.map() - JavaScript | MDN[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Process each element of an array or object to get a new array.

For array, each element is can be translated.

For object, it can be used to map an object key to an array.

Detail and example at jQuery documentation. jQuery.map() | jQuery API Documentation[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100