I have a object like this :
totalProduct = [{name: "A", string: "Hello"}, {name: "A", string: "Hello"}, {name: "B", string: "Hey"}]; I calculate the total of the same name. So far everything is fine, I did this :
let counts = {}; for (const i in totalProduct) { const nameObject = totalProduct[i].name; const stringObject = totalProduct[i].string; counts[nameObject] = (counts[nameObject]||0) + 1; } The output is this :
{A:2, B:1} BUT, i would like to have this output (Put the object value inside of an array with a string), but i don't know how to do this :(
{A:[2, 'Hello'], B:[1, 'Hey']} I'have tried to do this but it's not working :
let counts = {}; for (const i in totalProduct) { const nameObject = totalProduct[i].name; const stringObject = totalProduct[i].string; counts[nameObject] = [(counts[nameObject]||0) + 1, stringObject]; }
HelloandHey, where do they originate?const stringObject = totalProduct[i].string;totalProduct = [{name: "A"}, {name: "A"}, {name: "B"}];this does not have a propertystringAon itsnameproperty, they will also have the samestringvalue? Could there be{ name : 'A', string: 'Hello' }and{ name : 'A', string: 'Hey' }?