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}]

The array shown in the comment has more than one object in it. It has 12 objects. Can you show us exactly what the array with one element should look like? Also, do you really need an array with one object or can you just create one object?

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

Then definitely the map method is not what you want. It only returns an new array with the same number of elements as the original array. You have an array with one element (an object) shown above. Stick with forEach or learn about the reduce method.

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

It is just the way the method is implemented. You give it an array of 12 elements, it will return an array of 12 elements. What is important is that the 12 elements returned can be completely different than the original 12 elements. What those returned elements look like is determined by the callback function used. That function will be called on each element. The function specified will return a “transformed” value for each element pass to it.

See our map challenge or MDN’s map documentation for more information. If you still are confused after reading through both of these, reply back with more questions regarding what you did not understand from these sources.

1 Like

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