查找最单击的'在javaScript中给定的一系列变体中的变体

发布于 2025-02-06 22:43:26 字数 929 浏览 0 评论 0原文

我有一系列称为articles的对象。一篇文章具有3个重要属性。 名称,<代码>单击和variantsvariants属性也是包含name的对象数组(它对应于数组articles中的文章)。 最有效的方法找到每个文章最单击的变体是什么?

我认为也许有某种方法可以使用地图或传播操作员,但我不了解语法。我认为我可以为每组最单击的文章创建一个“词典”,但是有一个简单的方法吗?

输入:

[
 {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"]},
]

所需的输出:

{name: "article1",
clicks: 10,
variants: ["article2", "article3"],
mostClickedVariant: "article3"}

{name: "article2",
clicks: 7,
variants: ["article1", "article3"],
mostClickedVariant: "article3"},

等。每篇文章

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2025-02-13 22:43:26

此版本不会突变原始版本,只需返回带有新属性的副本即可。它还使用一种更有效的方法来查找最大值,将列表减少到o(n)时间,而不是使用o(n log n) sort

const maxBy = (fn) => (xs) => xs .reduce (
  ({m, mx}, x, _, __, v = fn (x)) => v > m ? {m: v, mx: x} : {m, mx}, 
  {m: -Infinity}
) .mx

const addMostClicked = (
  articles, 
  index = new Map (articles .map (({name, clicks}) => [name, clicks])),
  findMax = maxBy((x) => index .get (x))
) => articles .map (
  (article) => ({... article, mostClickedVariant: findMax (article .variants)})
)

const articles = [{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"]}]

console .log (addMostClicked (articles))
.as-console-wrapper {max-height: 100% !important; top: 0}

我们首先创建一个索引将文章名称映射到他们的点击计数中,然后使用助手maxby,我们创建一个函数,findmax,可以找到与最大的名称相关的名称点击计数。

然后,我们只需映射元素,用新的mostClickedVariant通过调用findmax用变体名称列表确定的属性。

This version does not mutate the original, just returns a copy with the new properties added. It also uses a more efficient way to find maxima, reducing the list to its maximum in O (n) time, rather than using an O (n log n) sort.

const maxBy = (fn) => (xs) => xs .reduce (
  ({m, mx}, x, _, __, v = fn (x)) => v > m ? {m: v, mx: x} : {m, mx}, 
  {m: -Infinity}
) .mx

const addMostClicked = (
  articles, 
  index = new Map (articles .map (({name, clicks}) => [name, clicks])),
  findMax = maxBy((x) => index .get (x))
) => articles .map (
  (article) => ({... article, mostClickedVariant: findMax (article .variants)})
)

const articles = [{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"]}]

console .log (addMostClicked (articles))
.as-console-wrapper {max-height: 100% !important; top: 0}

We first create an index mapping the article names to their click counts, and, using the helper maxBy, we create a function, findMax that will find the name associated with the largest click count.

Then we simply map over the elements, returning copies with new mostClickedVariant properties determined by calling findMax with the list of variant names.

脸赞 2025-02-13 22:43:26

尝试一下(我在以下代码段中添加了所有描述性评论)

// Input array
const arr = [
  {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"]},
];

// Iterating over an input array.
const res = arr.map(obj => {
  // Fetching variant objects in an array based on the variants name we have in the array. 
  const variantArr = obj.variants.map(variant => arr.find(o => o.name === variant));
  // Getting most clicked variant object by sorting the variantArr in desc order based on the clicks.
  const mostClickedVariant = variantArr.sort((a, b) => b.clicks - a.clicks)[0];
  // creating a new property 'mostClickedVariant' in each obejct and assigning the name of the most clicked variant.
  obj.mostClickedVariant = mostClickedVariant.name;
  return obj;
});

// Expected output
console.log(res);

Try this (I added all the descriptive comments in the below code snippet) :

// Input array
const arr = [
  {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"]},
];

// Iterating over an input array.
const res = arr.map(obj => {
  // Fetching variant objects in an array based on the variants name we have in the array. 
  const variantArr = obj.variants.map(variant => arr.find(o => o.name === variant));
  // Getting most clicked variant object by sorting the variantArr in desc order based on the clicks.
  const mostClickedVariant = variantArr.sort((a, b) => b.clicks - a.clicks)[0];
  // creating a new property 'mostClickedVariant' in each obejct and assigning the name of the most clicked variant.
  obj.mostClickedVariant = mostClickedVariant.name;
  return obj;
});

// Expected output
console.log(res);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文