-5

I want to create a new variable that will count all the names and them as a assignments says but I am kind of stuck.

I have an array with names.

names = [Jan, Jan, Jana]

I would like to count these names, which is quite easy but what I am struggling with is putting them as the assignment says below.

const dtoOut = {
 chartData: {
    all: [
     {label: "Jan", value: 2},
     {label: "Jana", value: 1},

any help?

Ner0ess
  • 3
  • 4
  • Is `names = [Jan, Jan, Jana]` actually `names = ["Jan", "Jan", "Jana"]`? Where are you stuck with your code that fulfills this task? Please share the code and check out [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). – ggorlen Dec 12 '22 at 20:09
  • 1
    When it comes to homework assignments you need to show your work. Currently all you show is what you have and the desired outcome but no effort – DᴀʀᴛʜVᴀᴅᴇʀ Dec 12 '22 at 20:10
  • Similar to: https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements – Sarah Dec 12 '22 at 20:17
  • If I posted my entire homework then I would get messages similar to yours about how I am supposed to only ask concrete questions and adjust the homework to be easy to read. The entire homework has over 500 lines .. This was the easiest way for me to ask the question and try to get help. To answer your question, the names array is a generated array with 100 random czech most popular names. I need to count each individually and put it into output as I have written up. – Ner0ess Dec 12 '22 at 20:29
  • No one is asking you to show the entire 500 lines of code. You show an array declared to a variable and the end object you're after. You show no code in between that shows to anyone you've tried to convert it yourself beyond the request. This comes back to 1) maybe read the link to how to ask homework assignments 2) learning how to isolate code (very important for future testing/debugging) 3) possibly take a step back and learn what and how to write Pseudo Code. – DᴀʀᴛʜVᴀᴅᴇʀ Dec 12 '22 at 20:43

1 Answers1

-3

if your keys do not change this is one easy way

const result = {
 chartData: {
   all: names.reduce((acc, curr) => {
     const obj = acc.find(o => o.label === curr);
     if (obj) {
       obj.value += 1;
     } else {
       acc.push({label: curr, value: 1});
     }
     return acc;
   }, [])
 }
};
aarriiaann
  • 56
  • 8
  • -1 You aren't helping the OP by doing his work for them so when you say "easy" that is an under statement because if it was they'd be able to do it. – DᴀʀᴛʜVᴀᴅᴇʀ Dec 12 '22 at 20:13
  • [how-do-i-ask-and-answer-homework-questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – mplungjan Dec 12 '22 at 20:21
  • @aarriiaann Thank you so much bro! I also don´t see the reason for hate... I have completed 90% of the homework alone and when I get stuck I prefer to ask it here than searching half the hour through the documentation. This is the way I learn the fastest as well. Once again thank you very much! – Ner0ess Dec 12 '22 at 20:23