Last active
October 8, 2021 18:19
-
-
Save peteplays/2da11f7cbd3499fd32cb81f10b8f743f to your computer and use it in GitHub Desktop.
Code Talk - Reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colorArr = ['red', 'blue', 'green', 'orange']; | |
const reduceArr = colorArr.reduce((accumulator, currentItem, currentIndex, entireArrayIteratingOn) => { | |
accumulator.push(currentItem) | |
return accumulator | |
}, []) | |
console.log(reduceArr) | |
const reduceObj = colorArr.reduce((accumulator, currentItem, currentIndex, entireArrayIteratingOn) => { | |
accumulator[currentItem] = `item-${currentIndex}` | |
return accumulator | |
}, {}) | |
console.log(reduceObj) | |
const colorArr = ['red', 'blue', 'green', 'orange']; | |
const mapWithIf = colorArr.map((currentColor) => { | |
if (currentColor === colorArr[1]) { | |
return currentColor; | |
} | |
}); | |
console.log(mapWithIf); | |
const mapWithIfFilter = colorArr.map((currentColor) => { | |
if (currentColor === colorArr[1]) { | |
return currentColor; | |
} | |
}) | |
.filter(t => t); | |
console.log(mapWithIfFilter); | |
const filterOne = colorArr.filter(currentColor => currentColor === colorArr[1]) | |
console.log(filterOne); | |
const forEachIf = [] | |
colorArr.forEach((currentColor) => { | |
if (currentColor === colorArr[1]) { | |
forEachIf.push(currentColor); | |
} | |
}); | |
console.log(forEachIf); | |
const reduceIf = colorArr.reduce((accumulator, currentColor) => { | |
if (currentColor === colorArr[1]) { | |
accumulator.push(currentColor) | |
} | |
return accumulator | |
}, []) | |
console.log(reduceIf) | |
const personObjOne = { | |
name:'jane', | |
hair: 'blonde', | |
eyes: 'blue', | |
height: 5.7, | |
awesome: true, | |
}; | |
const personObjTwo = { | |
name:'john', | |
hair: 'brown', | |
eyes: 'green', | |
height: 6, | |
awesome: true, | |
}; | |
const reduceArrayIf = [personObjOne, personObjTwo].reduce((accumulator, currentPerson) => { | |
const { hair } = currentPerson | |
if (hair === 'blonde') { | |
accumulator.push(currentPerson) | |
} | |
return accumulator | |
}, []); | |
console.log(reduceArrayIf); | |
const reduceNewObject = [personObjOne, personObjTwo].reduce((accumulator, currentPerson) => { | |
const { awesome, name } = currentPerson | |
if (awesome) { | |
accumulator.push(name) | |
} | |
return accumulator | |
}, []); | |
console.log(reduceNewObject) | |
const mapNewObj1 = [personObjOne, personObjTwo].map((currentPerson) => { | |
const { awesome, name } = currentPerson | |
if (awesome) { | |
return name | |
} | |
}); | |
console.log(mapNewObj1) | |
const mapNewObj2 = [personObjOne, personObjTwo].map((currentPerson) => { | |
const { name } = currentPerson | |
if (name === 'jane') { | |
return name | |
} | |
}); | |
console.log(mapNewObj2) | |
const mapNewObj3 = [personObjOne, personObjTwo].reduce((accumulator, currentPerson) => { | |
const { name } = currentPerson | |
if (name === 'jane') { | |
accumulator.push(name) | |
} | |
return accumulator | |
},[]); | |
console.log(mapNewObj3) | |
const partialPerson1 = { | |
name: 'jimi', | |
height: 5, | |
} | |
const partialPerson2 = { | |
eyes: 'blue', | |
hair: 'brown', | |
} | |
const partialPerson3 = { | |
eyes: 'green', | |
hair: 'blonde', | |
} | |
const newPerson = {...partialPerson1, ...partialPerson2} | |
console.log(newPerson) | |
const personIf = [partialPerson2, partialPerson3].reduce((accumulator, partialPerson) => { | |
const { eyes } = partialPerson | |
if (eyes === 'green') { | |
accumulator = { ...partialPerson } | |
} | |
return accumulator | |
}, {}); | |
console.log(personIf) | |
const personOptions = [partialPerson2, partialPerson3] | |
const personBuild = personOptions.reduce((accumulator, partialPerson, index, allPassedInItems) => { | |
const { eyes } = partialPerson | |
// index | |
// personOptions -> allPassedInItems | |
// allPassedInItems | |
if (eyes === 'green') { | |
accumulator = { ...accumulator, ...partialPerson } | |
} | |
return accumulator | |
}, partialPerson1); | |
console.log(personBuild) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment