Skip to content

Instantly share code, notes, and snippets.

@bhuizi
Created July 8, 2017 08:06
Show Gist options
  • Save bhuizi/a49a70f00d2b598c636cf135e8c4b576 to your computer and use it in GitHub Desktop.
Save bhuizi/a49a70f00d2b598c636cf135e8c4b576 to your computer and use it in GitHub Desktop.
array reducer
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