-1

I'm trying to iterate through array values in my project.

If I iterate through Array.map() it gives me empty array, but if I iterate with for...of loop I get the right results (new array with values).

const items = await Item.find({ itemId: items.map(item => item) });

for (item of items) {
    itemsArr.push(await Item.find({ itemId: item };
}
Michal
  • 11
  • 3

1 Answers1

-2

Array.map is best used when you want to implement a function for every item you loop in

var array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

The best case to solve your array.map problem would be

      Promise.all(items.map( async (e) => {
 itemsArr.push(await Item.find({ itemId: e };
}));
rawk
  • 110
  • 5