JavaScript Javascript refactor map example to use reduce

As an exercise I'm trying to convert a map example to use reduce:

const numbers = [1, 2, 3, 4];

numbers.map(function(a,b) {
    return a+b;
});
// [1, 3, 5, 7]

my attempt for same example with reduce:

numbers.reduce(
  (sum, elem) => {
    sum.push(elem + elem); // how can one add the index of array to the number instead of doubling it?
    return sum;
  }, []);
// [2, 4, 6, 8], instead I want: [1, 3, 5, 7]
Answer:1

Your result with reduce. Pass the third parameter, which is the index

const numbers = [1, 2, 3, 4];


var arr = numbers.reduce(
  (sum, elem, index) => {
    sum.push(elem + index);
    return sum;
  }, []);

console.log(arr);
Answer:2

My rest API returns with the following json content: [{ "key": "apple", "value": "green" }, { "key": "banana", "value": "yellow" }] I am iterating through the list with this code: ...

My rest API returns with the following json content: [{ "key": "apple", "value": "green" }, { "key": "banana", "value": "yellow" }] I am iterating through the list with this code: ...

  1. convert json array to java list of objects
  2. convert json array to string
  3. convert json array to list java
  4. convert json array to list
  5. convert json array to list c#
  6. convert json array to string javascript
  7. convert json array to xml c#
  8. convert json array to java object
  9. convert json array to string java
  10. convert json array to csv
  11. convert json array to map in java
  12. convert json array to object
  13. convert json array to datatable c#
  14. convert json array to string c#
  15. convert json array to list of map in java
  16. convert json array to java list using jackson
  17. convert json array to string online
  18. convert json array to object c#
  19. convert json array to dictionary c#
  20. convert json array to list c# newtonsoft

I have found there a nice and working perfectly fine tutorial to play with music with a web library. Everything is working fine but I would like to have the "average value" of the music I play. Let ...

I have found there a nice and working perfectly fine tutorial to play with music with a web library. Everything is working fine but I would like to have the "average value" of the music I play. Let ...

I have a JSON object: var info=JSON.parse(server_response); I run console.log(info); And got this output: [ [ [ "Dipu", "Mondal", "O Positive", "017xxxx", "...

I have a JSON object: var info=JSON.parse(server_response); I run console.log(info); And got this output: [ [ [ "Dipu", "Mondal", "O Positive", "017xxxx", "...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. cancun
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis

I'm trying to make kind of typewriting effect in HTML5. For rendering and puting it all together , I'm using this pixi.js plugin. So far I have been able to make it work (partially), although it ...

I'm trying to make kind of typewriting effect in HTML5. For rendering and puting it all together , I'm using this pixi.js plugin. So far I have been able to make it work (partially), although it ...