Tengo este array de objetos
const arr = [
{name: "Black", hex: "#000000"},
{name: "Oriental Olive", hex: "#445533"},
{name: "Wicked Green", hex: "#9bca47"},
{name: "Dreamless Sleep", hex: "#111111"},
{name: "Leaf", hex: "#71aa34"},
{name: "Cucumber Milk", hex: "#c2f177"},
{name: "Emerald Forest", hex: "#224347"},
{name: "Sea", hex: "#3c9992"},
{name: "Black", hex: "#000000"},
{name: "Black", hex: "#000000"},
];
Y lo regreso con un reduce para contar los repetidos
const res = Array.from(
arr.reduce((m, {
name,
hex
}) => {
var regresar = m.set(name, (m.get(name) || 0) + 1, hex)
return regresar
}, new Map()),
([name, count, hex]) => ({
name,
count,
hex
})
);
//Regresa el hex como undefined
console.log(res);
Regresa esto
0: {name: "Black", count: 3, hex: undefined}
1: {name: "Oriental Olive", count: 1, hex: undefined}
2: {name: "Wicked Green", count: 1, hex: undefined}
3: {name: "Dreamless Sleep", count: 1, hex: undefined}
4: {name: "Leaf", count: 1, hex: undefined}
5: {name: "Cucumber Milk", count: 1, hex: undefined}
6: {name: "Emerald Forest", count: 1, hex: undefined}
7: {name: "Sea", count: 1, hex: undefined}
Como puedo hacer para que me regrese el tercer valor bien? O alguna otra forma de contar los repetidos y crear un nuevo array de objetos con los (name) repetidos contados
algo asi {name: "Black", count: 3, hex: "#000000"}