Yahoo India Web Search

Search results

  1. Learn how to use map() to create a new array from calling a function for every array element. See examples, syntax, parameters, return value and browser support for map().

    • JavaScript Maps

      JavaScript Maps. Previous Next . A Map holds key-value pairs...

    • Overview
    • Syntax
    • Description
    • Examples
    • Browser compatibility
    • See also

    The map() method of Array instances creates a new array populated with the results of calling a provided function on every element in the calling array.

    Parameters

    callbackFn A function to execute for each element in the array. Its return value is added as a single element in the new array. The function is called with the following arguments: element The current element being processed in the array. index The index of the current element being processed in the array. array The array map() was called upon. thisArg Optional A value to use as this when executing callbackFn. See iterative methods.

    Return value

    A new array with each element being the result of the callback function.

    The map() method is an iterative method. It calls a provided callbackFn function once for each element in an array and constructs a new array from the results. Read the iterative methods section for more information about how these methods work in general.

    callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.

    The map() method is generic. It only expects the this value to have a length property and integer-keyed properties.

    Since map builds a new array, calling it without using the returned array is an anti-pattern; use forEach or for...of instead.

    Mapping an array of numbers to an array of square roots

    The following code takes an array of numbers and creates a new array containing the square roots of the numbers in the first array.

    Using map to reformat objects in an array

    The following code takes an array of objects and creates a new array containing the newly reformatted objects.

    Using parseInt() with map()

    It is common to use the callback with one argument (the element being traversed). Certain functions are also commonly used with one argument, even though they take additional optional arguments. These habits may lead to confusing behaviors. Consider: While one might expect [1, 2, 3], the actual result is [1, NaN, NaN]. parseInt is often used with one argument, but takes two. The first is an expression and the second is the radix to the callback function, Array.prototype.map passes 3 arguments: the element, the index, and the array. The third argument is ignored by parseInt — but not the second one! This is the source of possible confusion. Here is a concise example of the iteration steps: To solve this, define another function that only takes one argument: You can also use the Number function, which only takes one argument: See A JavaScript optional argument hazard by Allen Wirfs-Brock for more discussions.

    BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

    •Polyfill of Array.prototype.map in core-js

    •Indexed collections guide

    •Array

    •Array.prototype.forEach()

    •Array.from()

    •TypedArray.prototype.map()

  2. Learn how to create and use a Map object in JavaScript, which holds key-value pairs and remembers the insertion order. Compare Maps with Objects and see browser support and examples.

  3. May 29, 2024 · Learn how to create and use map objects in JavaScript, which are collections of key-value pairs. See examples of map methods, advantages, and supported browsers.

    • 4 min
  4. Mar 11, 2024 · Description. Map objects are collections of key-value pairs. A key in the Map may only occur once; it is unique in the Map 's collection. A Map object is iterated by key-value pairs — a for...of loop returns a 2-member array of [key, value] for each iteration.

  5. Jun 7, 2024 · Learn how to use the map () method to iterate over an array, apply a callback function to each element, and generate a new array. See examples of map () with square roots, squares, concatenation, and parseInt.

  6. People also ask

  7. Mar 31, 2021 · The .map() function is a built-in array method that allows you to iterate over an array and modify its elements using a callback function. Learn the syntax, examples, and arguments of the .map() function with interactive scrims and a book recommendation.

  1. People also search for