如何使用 JavaScript select 字符串的最后 4 个字符?

How can I select the last 4 characters of a string using JavaScript?

此代码选择数据 ID 之间的最高数值并为其添加石灰背景。顺便说一句,这些数字是十六进制值。 我的问题是十六进制值是最后 4 个值的问题,但我的代码采用了整个字符。我怎样才能让我的代码只对最后 4 个字符起作用?

对不起,这对你来说可能很容易解决,但我试了一遍又一遍,还是没成功。

十六进制转十进制:

dc61 = 56417

dc62 = 56418

dc63 = 56419

dc64 = 56420

 maxData = $(".answers li[data-id]").get ().reduce  ( (maxObj, crrntNode) => {
 
    var idVal   = parseInt ( $(crrntNode).data("id"), 16) ; 

    if (idVal > maxObj.value) {
        maxObj.value  = idVal;
        maxObj.node   = crrntNode;
    }
    return maxObj;
  },
  {value: 0, node: null}
);
$("body").append (`<p>The highest data-id value was ${maxData.value}.</p>`)
$(maxData.node).css ("background", "lime");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="question-text" class="question sp-element border-radius active">What is favorite colour?</div>
          <ul class="answers" id="answers">
            <li data-id="58b9890062279282090ddc61" class="answer sp-element border-radius active">Purple</li>
            <li data-id="58b9890062279282090ddc63" class="answer sp-element border-radius active">Blue</li>
            <li data-id="58b9890062279282090ddc64" class="answer sp-element border-radius active">Yellow</li>
            <li data-id="58b9890062279282090ddc62" class="answer sp-element border-radius active">Red</li>
          </ul>

使用String.substr(startPosition, numChars)方法:

var myString = "abcdefg";
console.log(myString.substr(-4, 4)); // Start 4 chars before end and take 4 chars

通过使用substr您可以获得数据属性的最后4个字符。所以,而不是:

var idVal = parseInt($(crrntNode).data("id"), 16);

你可以这样做:

var nodeId = $(crrntNode).data("id");
var idVal = parseInt(nodeId.substr(nodeId.length - 4), 16);

 maxData = $(".answers li[data-id]").get ().reduce  ( (maxObj, crrntNode) => {
    var nodeId = $(crrntNode).data("id");
    var idVal = parseInt(nodeId.substr(nodeId.length - 4), 16); 

    if (idVal > maxObj.value) {
        maxObj.value  = idVal;
        maxObj.node   = crrntNode;
    }
    return maxObj;
  },
  {value: 0, node: null}
);
$("body").append (`<p>The highest data-id value was ${maxData.value}.</p>`)
$(maxData.node).css ("background", "lime");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="question-text" class="question sp-element border-radius active">What is favorite colour?</div>
          <ul class="answers" id="answers">
            <li data-id="58b9890062279282090ddc61" class="answer sp-element border-radius active">Purple</li>
            <li data-id="58b9890062279282090ddc63" class="answer sp-element border-radius active">Blue</li>
            <li data-id="58b9890062279282090ddc64" class="answer sp-element border-radius active">Yellow</li>
            <li data-id="58b9890062279282090ddc62" class="answer sp-element border-radius active">Red</li>
          </ul>

如果我正确理解了你的问题(我不确定我是否理解),你只想获取已知字符串的最后 4 个字符(转换为十进制,但这不是你的问题)。

在 javascript 中,您可能希望使用 substring api。例如,要获取任意字符串的最后 4 位数字,您可以这样做:

var str = "abcdefghijklmnop";
var substr = str.substring(str.length-4, str.length);

更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring