2

I'm trying to do a counter object in JavaScript like so: EDIT: I've added an example

    let profile={skill:['javascript','javascript','html','css','css']}
    let objectCount={}

profiles.map(profile => {
        profile.skills.map(skill => {
          skill = skill.toLowerCase();
          if (!(skill in objectCount)) {
            objectCount = { ...objectCount, [skill]: 1 };
          } else {
            objectCount.skill = objectCount.skill + 1;
          }
        });
      });
    }

I have array of profiles and inside them an array of skills which is : 'javascript','html','css',etc and I wish to make an objectCount that will be:

objectCount={
  'html':1,
  'javascript:2,
   'css':2
}

but for some reason the line objectCount.skill=objectCount.skill+1 doesnt work, because I get an object with 1 in every single key, does anybody know whats my error?

2
  • 1
    It’s helpful if you post all the code necessary to reproduce your error. You should make this a runnable snippet. Commented Apr 6, 2019 at 17:07
  • this is the entire code , you want me to write you a sample profile with an array of skills? Commented Apr 6, 2019 at 17:08

2 Answers 2

4

You're trying to increment the value of a key skill on the objectCount object that does not exist. Instead you want to increment the value of the key that has the name of the skill variable. So you have to use objectCount[skill] instead of objectCount.skill:

let profiles = [
  {
    skills: ['html', 'js']
  },
  {
    skills: ['html', 'php']
  }
]
let objectCount = {}

profiles.map(profile => {
  profile.skills.map(skill => {
    skill = skill.toLowerCase();
    if (!(skill in objectCount)) {
      objectCount = { ...objectCount, [skill]: 1 };
    } else {
      objectCount[skill] = objectCount[skill] + 1;
    }
  });
});
console.log(objectCount);

Sign up to request clarification or add additional context in comments.

6 Comments

Oh thank you so much for the answer, but could you explain to me why the skill does not exist? I tried to run this at the javascript console: object={'alex':3} Object { alex: 3 } object.alex=object.alex+1 4 object.alex 4
Because you define the object as objectCount={}, so there's no key in the object named skill.
yes but the if checks if the key doesnt exist, and if it does it goes to the else, which is why the skill should be in the object already, that's what confuses me.
again: If the variable skill is "html" for example, the line objectCount.skill does NOT get the html key of the object, but instead it tries to get a key of the name skill, and a key of the name skill is not defined on that object.anywhere in your code.
Yes, if you want to acces an object key by variable, you can do it with the array-like square bracket notation as in my example code.
|
0

Another way of creating this counter map is to use the reduce function instead of map.

For example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#counting_instances_of_values_in_an_object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.