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

时间:2017-12-20 21:31:41

标签: javascript jquery greasemonkey tampermonkey

此代码选择数据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");
&#13;
<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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

通过使用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>

答案 2 :(得分:1)

如果我理解你的问题(我不确定),你只想取一个已知字符串的最后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