I have an array of objects called articles
. An article has 3 important properties. name
, clicks
, and variants
. The variants
property is also an array of objects that contain name
(It corresponds to an article in the array articles
). What is the most efficient way to find each article's most clicked variant?
I thought maybe there was some way to use a map or spread operator, but I don't understand the syntax. I think I could create a 'dictionary' with the most clicked article for each set, but is there an easier way?
Input:
[
{name: "article1",
clicks: 10,
variants: ["article2", "article3"]},
{name: "article2",
clicks: 7,
variants: ["article1", "article3"]},
{name: "article3",
clicks: 15,
variants: ["article1", "article2"]},
{name: "article4",
clicks: 3,
variants: ["article5"]},
{name: "article5",
clicks: 6,
variants: ["article4"]},
]
Desired output:
{name: "article1",
clicks: 10,
variants: ["article2", "article3"],
mostClickedVariant: "article3"}
{name: "article2",
clicks: 7,
variants: ["article1", "article3"],
mostClickedVariant: "article3"},
etc. for each article