Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.find() return item and index

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}
  }
})
like image 410
K20GH Avatar asked Sep 08 '26 04:09

K20GH


1 Answers

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 };
}
like image 164
Petr Broz Avatar answered Sep 10 '26 18:09

Petr Broz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!