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

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

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

问题

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

  1. <div class="repeater-item" data-index="0-0"></div>
  2. <div class="repeater-item" data-index="0-1"></div>
  3. <div class="repeater-item" data-index="0-2"></div>
  4. <div class="repeater-item" data-index="1-0"></div>
  5. <div class="repeater-item" data-index="1-1"></div>
  6. <div class="repeater-item" data-index="2-0"></div>
  7. <div class="repeater-item" data-index="2-1"></div>

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

  1. const getIndex = function() {
  2. var num = $(".repeater-item").map(function() {
  3. return $(this).data('index');
  4. }).get();
  5. return Math.max.apply(Math, num); // 失败
  6. }

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

英文:

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

  1. &lt;div class=&quot;repeater-item&quot; data-index=&quot;0-0&quot;&gt;&lt;/div&gt;
  2. &lt;div class=&quot;repeater-item&quot; data-index=&quot;0-1&quot;&gt;&lt;/div&gt;
  3. &lt;div class=&quot;repeater-item&quot; data-index=&quot;0-2&quot;&gt;&lt;/div&gt;
  4. &lt;div class=&quot;repeater-item&quot; data-index=&quot;1-0&quot;&gt;&lt;/div&gt;
  5. &lt;div class=&quot;repeater-item&quot; data-index=&quot;1-1&quot;&gt;&lt;/div&gt;
  6. &lt;div class=&quot;repeater-item&quot; data-index=&quot;2-0&quot;&gt;&lt;/div&gt;
  7. &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 ....)

  1. const getIndex = function()
  2. {
  3. var num = $(&quot;.repeater-item&quot;).map(function() {
  4. return $(this).data(&#39;index&#39;);
  5. }).get();
  6. return Math.max.apply(Math, num); // Fail
  7. }

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:

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

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

  1. const getMaxIndex = () =&gt; {
  2. const sorted = $(&quot;.repeater-item&quot;).sort(function (a, b) {
  3. const indexANumeric = +$(a).data(&#39;index&#39;).replace(&quot;-&quot;, &quot;&quot;);
  4. const indexBNumeric = +$(b).data(&#39;index&#39;).replace(&quot;-&quot;, &quot;&quot;);
  5. return indexBNumeric - indexANumeric;
  6. }).get();
  7. return $(sorted[0]).data(&#39;index&#39;);
  8. }

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:

确定