Tips in how to sort out this algorithm

I am trying to solve this problem, you have to return an array of the items that are in both arrays given:

For example

console.log(findMatches([4,9,5], [9,4,9,8,4])) // returns [4,9] because 4 and 9 are in both arrays

My code already solves this first test, but the issue is, I have to return also the numbers x amount of times, of in other words *"Each element in the result should appear as many times as it shows in both arrays."

For example in

console.log(findMatches([1,2,2,1], [2,2])) // should return [2,2] because it is repetated in both arrays two times!! 

That´s what i need to do now. Because this last test i don´t pass it. My function returns just [2] because it doesn´t count yet how many times is repeated the number. This is what I don´t know how to do.

function match(arr1, arr2){
    
    let matchesArr = [];

    arr1.map((item) => {
        return arr2.map((item2) => {
          if (item === item2){
            matchesArr.push(item)
          }
        })
      })
      let filtered = [...new Set(matchesArr)]

      return filtered
}

console.log(match([1,2,2,1], [2,2])); // WRONG  should return [2,2], returns [2]
console.log(match([4,9,5], [9,4,9,8,4])); // CORRECT should return [4,9], returns [4,9]

A Set can only have unique values…

Description
A value in the Set may only occur once; it is unique in the Set’s collection.

I see, but I was already thinking to do add the repeated values afterwards.

The way you are using map is horrible.
The way you are using it, I should be forEach() method.

Considering that a set can only have each value one time, would the array before making it a set work?

You could filter the arrays and then use concat. To remove repetition, create a new Set

Looks to me like you’re not using map correctly. Map is used to convert every element of the array into something else. For example, if I have an array of numbers [1, 2, 3, 4] and I need to know what all those numbers are squared, I would use map on it:
[1, 2, 3, 4].map(num => num ** 2); // returns [1, 4, 9, 16]. If you just want to do something for every element in the array without returning a new array, you can use forEach as @ilenia suggested, or a simple for loop.

When I need to count how many times something appears in the array I like to make an object to store the count of each item. It eliminates the need to loop through arrays multiple times.