Array .map only on elements that fit a .filter

I want to loop through an array and update elements in place. I only want to loop through elements that fit a filter. How would I do this?

naive approach:

arr.forEach(element => {
    if (element.val > 5) {
         element = 9;
    }
});

I expect that with .map and .filter I should be able to do this more efficiently and inline.

No need of filter(), just use Array.prototype.map() like the following:

var arr = [5, 10, 15]
arr = arr.map(element => element > 5? element = 9 : element);
console.log(arr);

Simplify your JavaScript – Use .map(), .reduce(), and .filter(), Just like .map() , .reduce() also runs a callback for each element of an array. replace some of your for loops with .map() , .reduce() , .filter() where it seems to fit​. Simplify your JavaScript – Use .map(), .reduce(), and .filter() What if you have an array, but only want some of the elements in it? .filter() where it seems to fit. I guarantee your

Since the question asks for an "in place" update, this approach updates the existing array:

let arr = [20, 5, 6, 1];
arr.forEach((e, i) => { if (e > 5) arr[i] = 9; })    
console.log(arr)

JavaScript Array Filter: Filtering Array Elements Based on a Test , The filter() method includes only the element in the result array if the element Like other iterative methods of the Array object such as every(), some(), map()  It does exactly what it sounds like: It takes an array, and filters out unwanted elements. Like map, filter is defined on Array.prototype. It's available on any array, and you pass it a callback as its first argument. filter executes that callback on each element of the array, and spits out a new array containing only the elements for which the

If you don't care much about performance and want to have a little fun:

Array.prototype.mutate = function (cb) {
    var updates = {};
    this.forEach(function (element, index) {
        var wrapper = {
            value: function () {
                return element;
            },
            update: function (value) {
                updates[index] = value;
            }
        };
        cb(wrapper, index);
    });
    var self = this;
    Object.keys(updates).forEach(function (index) {
        var value = updates[index];
        self[index] = value;
    });
};

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

a.mutate(function (wrapper) {
    if (wrapper.value() % 2 === 0) {
        wrapper.update(wrapper.value() * 2);
    }
});

console.log(a); //[ 1, 4, 3, 8, 5, 12, 7, 16, 9, 20 ]

JavaScript Map, Reduce, and Filter, var new_array = arr.map(function callback(element, index, array) { // Return value for new_array }[, thisArg]). In the callback, only the array  Filter receives the same arguments as map, and works very similarly. The only difference is that the callback needs to return either true or false. If it returns true then the array keeps that

Array Methods Explained : Filter vs Map vs Reduce vs Foreach, Filter let you provide a callback for every element and returns a filtered array. The main difference between forEach and filter is that forEach just loop over the  filter () calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been

How to Use Map, Filter, & Reduce in JavaScript, filter executes that callback on each element of the array, and spits out a new array containing only the elements for which the callback returned  A new array with each element being the result of the callback function. Description. map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values (including undefined).

How to simplify your codebase with map(), reduce(), and filter() in , Well, this is the only one not about map/reduce/filter, but it's so compact that it was The filter() method creates a new array with all elements that pass the test​  Stop Array.forEach and start using filter, map, some, reduce functions. Array.Filter returns a new array of elements which pass the given test functions. We need to write only odd number

Comments
  • you can filter the array first and then map it - arr.filter(...).map(...)
  • arr.filter(x => x > 5).map(x => 9)... although I'm not sure why you'd want this...
  • and which way do you need it? with a new array or mutate the old array?