Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
The problem with adding property to object

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!