I'm trying to convert this function to use reduce. I am halfway through. When the selected value is true I want to know the index of that element.
let options = [
{label: 'foo1', selected: false},
{label: 'foo2', selected: true},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
{label: 'foo2', selected: false},
];
const obj = options
.map((option, index) => (option.selected ? index : -1))
.filter((val) => val !== -1)[0];
Result: 1
My attempt is this:
const obj = options.reduce((acc, currentValue, index) => {
const i = currentValue["selected"] ? index : -1;
return acc.concat(i)
}, []); // [-1,1,-1,-1,-1]
How do I change the entire thing to use reduce?