How to map, reduce and filter a Set element using JavaScript ?

The map(), reduce() and filter() are array functions that transform the array according to the applied function and return the updated array. They are used to write simple, short and clean codes for modifying an array instead of using the loops.

  • map() method: It applies a given function on all the elements of the array and returns the updated array. It is the simpler and shorter code instead of a loop. The map is similar to the following code:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    arr = new Array(1, 2, 3, 6, 5, 4);
    for(int i = 0; i < 6; i++) {
        arr[i] *= 3;
    }

    chevron_right

    
    

    Syntax:

    array.map(function_to_be_applied)
    array.map(function (args) {
        // code;
    })
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    function triple(n){
        return n*3;
    }    
    arr = new Array(1, 2, 3, 6, 5, 4);
      
    var new_arr = arr.map(triple)
    console.log(new_arr);

    chevron_right

    
    

    Output: 
    [ 3, 6, 9, 18, 15, 12 ]
    
  • reduce() method: It reduces all the elements of the array to a single value by repeatedly applying a function. It is an alternative of using a loop and updating the result for every scanned element. Reduce can be used in place of the following code:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    arr = new Array(1, 2, 3, 6, 5, 4);
    result = 1
    for(int i = 0; i < 6; i++) {
        result = result * arr[i];
    }

    chevron_right

    
    

    Syntax:

    array.reduce(function_to_be_applied)
    array.reduce(function (args) {
        // code;
    })
    

    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    function product(a, b){
        return a * b;
    }
    arr = new Array(1, 2, 3, 6, 5, 4);
      
    var product_of_arr = arr.reduce(product)
    console.log(product_of_arr)

    chevron_right

    
    

    Output:

    720
  • filter() method: It filters the elements of the array that return false for the applied condition and returns the array which contains elements that satisfy the applied condition. It is a simpler and shorter code instead of the below code using a loop:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    arr = new Array(1, 2, 3, 6, 5, 4);
    new_arr = {}
    for(int i = 0; i < 6; i++) {
        if(arr[i] % 2 == 0) {
             new_arr.push(arr[i]);           
        }
    }

    chevron_right

    
    

    Syntax:

    array.filter(function_to_be_applied)
    array.filter(function (args) {
        // condition;
    })
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    arr = new Array(1, 2, 3, 6, 5, 4);
    var new_arr = arr.filter(function (x){
        return x % 2==0;
    });
      
    console.log(new_arr)

    chevron_right

    
    

    Output:

    [ 2, 6, 4 ]

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.