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?
>Solution :
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;
}, [])
}
};