Skip to content Skip to sidebar Skip to footer

How To Map Only Every Second Value In Array

I have some array of numbers: var arr = [1, 7, 1, 4]; I want to increase only every first value, such that the expected output would be: [2, 7, 2, 4] I tried some combination of m

Solution 1:

You can use map() and use second argument which is idnex to determine if it's at event index or not

let arr = [1, 7, 1, 4];
 
 let output = arr.map((n, index) => index % 2 === 0 ? n * 2 : n);
 
 console.log(output);

Post a Comment for "How To Map Only Every Second Value In Array"