Created
July 8, 2017 08:06
-
-
Save bhuizi/a49a70f00d2b598c636cf135e8c4b576 to your computer and use it in GitHub Desktop.
array reducer
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 answers = [ | |
{ | |
name: 'offense1', | |
value: 'offense1 value' | |
}, | |
{ | |
name: 'offense2', | |
value: 'offense2 value' | |
}, | |
{ | |
name: 'offense3', | |
value: 'offense3 value' | |
}, | |
{ | |
name: 'offense4', | |
value: 'offense4 value' | |
}, | |
{ | |
name: 'offense5', | |
value: 'offense5 value' | |
}, | |
]; | |
const selectedAnswers = ['offense2', 'offense5'] | |
const other = selectedAnswers.reduce((acc, select)=> { | |
answers.forEach(answer => { | |
if(answer.name === select){ | |
console.log(answer.value); | |
acc.push(answer.value); | |
} | |
}) | |
return acc; | |
}, []); | |
console.log(other); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment