gpt4 book ai didi

JavaScript - 如何增加计数

转载 作者:行者123 更新时间:2023-11-30 14:34:25 25 4
gpt4 key购买 nike

当我用当前下面的脚本给出一个 IP(10.31.68.0/22) 变量时,它将返回输出:10.31.68.1 - 10.31.68.10

var network = '10.31.68.0/22';
var IPstart = network.toString().substring(0,network.lastIndexOf('.')) + ".1";
var IPend = network.toString().substring(0,network.lastIndexOf('.')) + ".10";
var excludename = IPstart+"-"+IPend;

我正在寻找如下所示的内容(输出)。 68 占位符应增加 +1,如下所示。

10.31.68.1 - 10.31.68.10

10.31.69.1 - 10.31.69.10

10.31.70.1 - 10.31.70.10

10.31.71.1 - 10.31.71.10

最佳答案

你需要用循环拆分和连接值

var network = '10.31.68.0/22';
var IPstart = network.toString().substring(0,network.lastIndexOf('.')) + ".1";
var IPend = network.toString().substring(0,network.lastIndexOf('.')) + ".10";
var excludename = IPstart+"-"+IPend;
var upToNumber=4;
for(var i=0;i<upToNumber;i++){
var res_ipstart = IPstart.split(".");
res_ipstart[2]=parseInt(res_ipstart[2]) + i;
var ip_start = res_ipstart.join(".");

var res_ipend = IPend.split(".");
res_ipend[2]=parseInt(res_ipend[2]) + i;
var ip_end = res_ipend.join(".");

var output=ip_start+"-"+ip_end;
console.log(output);
}

关于JavaScript - 如何增加计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50635608/

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