math.max: 从字符串中找到最高的数值

huangapple go评论56阅读模式
英文:

math.max: find the highest numeric value from a string

问题

如何基于这样的索引获取最高的数字值?

<div class="repeater-item" data-index="0-0"></div>
<div class="repeater-item" data-index="0-1"></div>
<div class="repeater-item" data-index="0-2"></div>
<div class="repeater-item" data-index="1-0"></div>
<div class="repeater-item" data-index="1-1"></div>
<div class="repeater-item" data-index="2-0"></div>
<div class="repeater-item" data-index="2-1"></div>

在我的示例中,必须检索的最高索引是2-1,然后递增:1(2-2、2-3...)

const getIndex = function() {
    var num = $(".repeater-item").map(function() {
        return $(this).data('index');
    }).get();

    return Math.max.apply(Math, num); // 失败
}

这段代码可以成功获取索引,但无法根据我的示例计算最高索引。

英文:

How to get the highest numeric value based on indexes like this ?

&lt;div class=&quot;repeater-item&quot; data-index=&quot;0-0&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;repeater-item&quot; data-index=&quot;0-1&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;repeater-item&quot; data-index=&quot;0-2&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;repeater-item&quot; data-index=&quot;1-0&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;repeater-item&quot; data-index=&quot;1-1&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;repeater-item&quot; data-index=&quot;2-0&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;repeater-item&quot; data-index=&quot;2-1&quot;&gt;&lt;/div&gt;

In my example, the highest index that must be retrieved is 2-1 in order to increment, thereafter: 1 (2-2, 2,3 ....)

const getIndex = function()
{
    var num = $(&quot;.repeater-item&quot;).map(function() {
        return $(this).data(&#39;index&#39;);
    }).get();

    return Math.max.apply(Math, num); // Fail
}

This code fetches the indexes fine but fails to calculate the highest index based on my example

答案1

得分: 0

You can for example, sort an array using data-index as a criteria.
Then get the first element in the sorted array:

const getMaxIndex = () => {
  const sorted = $(".repeater-item").sort(function (a, b) {
    const indexANumeric = +$(a).data('index').replace("-", "");
    const indexBNumeric = +$(b).data('index').replace("-", "");
    return indexBNumeric - indexANumeric;
  }).get();
  return $(sorted[0]).data('index');
}
英文:

You can for example, sort an array using data-index as a criteria.
Then get first element in the sorted array:

  const getMaxIndex = () =&gt; {

    const sorted = $(&quot;.repeater-item&quot;).sort(function (a, b) {

      const indexANumeric = +$(a).data(&#39;index&#39;).replace(&quot;-&quot;, &quot;&quot;);
      const indexBNumeric = +$(b).data(&#39;index&#39;).replace(&quot;-&quot;, &quot;&quot;);

      return indexBNumeric - indexANumeric;

    }).get();

    return $(sorted[0]).data(&#39;index&#39;);
  }

huangapple
  • 本文由 发表于 2023年4月19日 19:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定