I have an array of data, and I need to return the data from an item in the array, as well as the index of the result:
My code below returns the data, but not the index so I don't believe this is working correctly at all.
Can anyone advise how I can achieve what I am attempting to do below please?
const result = design.data().items.find((e, i, a, arg) => {
if(e.id === this.props.match.params.item) {
return {item:e, index: i}
}
})
You can use findIndex instead of the find method.
const { items } = design.data();
const id = items.findIndex((e) => e.id === this.props.match.params.item);
if (id !== -1) {
return { item: items[id], index: id };
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With