Написать функцию которая считает кол-во каждой буквы в данной строке и выводит в консоль

На html странице должен быть input.
Написать функцию которая считает кол-во каждой буквы в данной строке и выводит в консоль.

Пример:

input: "hello world"
Выводит в html output: h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1

это все что я смог придумать

   function hw3() {
        const input = document.getElementById("input_search2");
        const inputValue = input.value;
        // 
        //
        const mapped = newArray.reduce((acc, item) => {
          if (acc.hasOwnProperty(item)) {
            acc[item]++;
          } else {
            acc[item] = 1;
          }
          return acc;
        }, {});
        // получится объект, у которого ключ - элемент исходного массива, а значение - количество повторений
        console.log(mapped);
        const result = Object.entries(mapped).filter(
          ([key, value]) => value > 1
        );
        // преобразуем объект mapped в массив и отфильтровываем значения меньше 2
        console.log(result);
      }
      hw3();


Ответы (2 шт):

Автор решения: Алексей Мейер

Примерно как-то так:

function hw3() {
   let symbols =  document.getElementById("input_search2").value.split('');
   let res = symbols.reduce((acc, el) => {
       acc[el] = (acc[el] || 0) + 1;
       return acc;
   }, {});

   console.log(res)
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<input id="input_search2" oninput="hw3()">
</body>
</html>

→ Ссылка
Автор решения: Qwertiy

document.querySelector('input').addEventListener('input', e => {
  var res = new Map()
  
  for (var ch of e.target.value) {
    res.set(ch, ~~res.get(ch) + 1)
  }
  
  document.querySelector('output').textContent = [...res].map(kvp => kvp.join(": ")).join("\n")
})
<input>
<pre><output></output></pre>

→ Ссылка