Substitute forEach() with map()

I am trying to replace a forEach method with map() but i am getting a weird result in the console. I don’t know why the same result is printed 12 times.

Here is what i wrote:

const prices = [10, 6, 4, 8, 10, 6, 3, 4, 4, 4, 9, 8];

let count = {};

const df = prices.map(iteration => {

  // checks no of iterations

  count[iteration] ? count[iteration]++ : (count[iteration] = 1);

  return count;

});

console.log(df); // logs: (12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

// prices.forEach(iteration => {

//   count[iteration] ? count[iteration]++ : (count[iteration] = 1);

// });

// console.log(count);

what do you want your map to do?
map returns an array where each element is the element of the starting array changed via the callback
are you sure you really want to use map here?

1 Like

i really don’t understand you here - I haven’t done much with map()

I read that map and forEach are pretty much the same; but map returns something. That’s why i tried substituting.

map returns a new array

what map does is something like this:

let newArr = [func(arr[0]), func(arr[1]), etc...]

the elements of the starting array are fed to the function argument of map, and map returns a new array where each element is changed by the function
your starting array has 12 elements, so your returned array has also 12 elements

what’s the output you want to get? Without knowing that there isn’t much I can suggest

1 Like

An output similar to the commented code - just a single array with one object

then you can’t use map

you could use reduce but I don’t know if I would suggest it if you have no experience using reduce

what’s wrong with using forEach?

this is too hard for me to grasp. i have seen something like that, but… maybe you could break it down a little bit more. If i take it the way i understand this pseudo-code, then i should be expecting undefined(s) in the console.

Nothing. I felt i could do the same with map. Haven’t learn’t reduce yet.

look at the function passed as argument of map:

the function argument of map returns count

so you get as result an array like [count, count, count...] (with count repeated 12 times)

you don’t want an array as output, so map doesn’t seem the thing you want to use

it seems complicated than i thought.

No, i want an output like [{3: 1, 4: 4, 6: 2, 8: 2, 9: 1, 10: 2}]

is what i mean’t but with the square brackets around it

Okay i get that, but could you please explain to me why there are twelve objects. i really didn’t fully understand what @ilenia was explaining.

Now you are saying the same thing. But the way i think of it is that if this is so, then i should have undefined as part of the output. I need a little bit more explanation on how it returns the same number of elements, please.

… made a lot of sense here, but i didn’t get it well enough

I made it complicated for myself. I understand it better now. Thank you so much @ilenia @camperextraordinaire