gpt4 book ai didi

javascript - 使用 javascript 检查数百万组

转载 作者:行者123 更新时间:2023-11-29 15:39:17 27 4
gpt4 key购买 nike

这里我返回一个数组,其中的值是这样格式化的

var myObj = {
"account": "5622555",
"account1": "156726",
"account3": "889338",
etc....
}

然后我格式化这个数组并将值分配给 HTML div,这是我正在使用的代码。

$.each(myObj, function(index, value){
$("#account" + index).html(value);
});

如何更改 value 以以百万为单位显示......就像这样

if(value > 1000000) {
if(value < 2000000) {
if(value != 1000000) {
$("#account" + index).html("1M+");
} else {
$("#account" + index).html("1M");
}
}
}
if(value > 2000000) {
if(value < 3000000) {
if(value != 2000000) {
$("#account" + index).html("2M+");
} else {
$("#account" + index).html("2M");
}
}
}
if(value > 3000000) {
if(value < 4000000) {
if(value != 3000000) {
$("#account" + index).html("3M+");
} else {
$("#account" + index).html("3M");
}
}
}

有什么方法可以更快地执行此类 if 语句,这样我就不必写出完整的数字?

最佳答案

最简单的方法是

$.each(myObj, function(index, value){
if(value > 999999)
$("#account" + index).html(String.prototype.replace.call(value, /(\d)(\d{6})/, "$1" + (value%1000000 > 0 ? 'M+' : 'M'), 'g'));
else
$("#account" + index).html(value);
});

关于javascript - 使用 javascript 检查数百万组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22384040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com