0

I have a large array which i want to apply a function into. normally I use map to apply function for all array elements. but I need to apply function for only first 15 elements of the array . is there a way to do that so i get a certain result I tried to slice the array then map around it but it didn't work thanks

Osama
  • 71
  • 7
  • what was the issue with slicing the array? if the array is large then the best approach would be to slice it. – Sahal Tariq Nov 14 '20 at 18:13
  • I think the answer to this question can be found here: https://stackoverflow.com/questions/34883068/how-to-get-first-n-number-of-elements-from-an-array – PetarBoshkoski Nov 14 '20 at 18:18

2 Answers2

0

please check this one.

var items = large_array.slice(0, 15).map(i => {
    return <myview item={i} key={i.id} />
}
shehanpathi
  • 312
  • 4
  • 15
0

If you can change an original array:

for (let idx = 0; idx < 15; idx++) {
    array[idx] = mapper(array[idx]);
}

Otherwise, version without modifying an original array:

array = array.map((item, idx) => idx < 15 ? mapper(item) : item);
z1ne2wo
  • 278
  • 1
  • 7