I'm practising in JS and trying to get list with amount of free emails.
var _ = require('lodash');
const freeEmailDomains = ['gmail.com', 'yandex.ru', 'hotmail.com'];
const getFreeDomainsCount = (emails) => {
let res = [];
res = emails.map((email) => {
let [, domain] = email.split('@');
return domain;
});
res = res.filter((domain) => freeEmailDomains.includes(domain));
res = res.reduce((freeDomains, domain) => {
console.log(domain);
if(!_.has(freeDomains,domain)) {
freeDomains[domain] = 0;
}
freeDomains[domain] += 1;
},
{});
return res;
}
const emails = [
'info@gmail.com',
'info@yandex.ru',
'info@hotmail.com',
'mk@host.com',
'support@hexlet.io',
'key@yandex.ru',
'sergey@gmail.com',
'vovan@gmail.com',
'vovan@hotmail.com',
];
getFreeDomainsCount(emails);
but my problem is that on the second step of reduce I see the erroe "TypeError: Cannot set properties of undefined (setting 'yandex.ru')" As I understand I cannot add key 'yandex.ru' to object 'freeDomains'. What is wrong?