Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Last active November 30, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KinoAR/ac9e6b4502ca916fa212bbfa7f770203 to your computer and use it in GitHub Desktop.
Save KinoAR/ac9e6b4502ca916fa212bbfa7f770203 to your computer and use it in GitHub Desktop.
Array.Reduce JavaScript Tutorial Code
'use strict'
//Object Reduce
let names = ['Ray', 'Hiro', 'Ross'];
let obj = names.reduce(function(finalObj, name, index){
//Create new property on object based on index (1, 2, 3) and assign name to it
index+=1;
finalObj[index] = name;
console.log(finalObj[index]);
return finalObj; //return the object for the next iteration
}, {});
console.log(obj);// {1: 'Ray', 2: 'Hiro', 3: 'Ross'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment