簡體   English   中英

如何從 JSON 獲取沒有端口的 IP 地址

[英]how can I get the IP address without port from JSON

這就是我所擁有的:

[
     "218.200.188.34:13310",
     "218.200.188.46:11295",
     "218.200.188.34:17357"
]

這就是我需要的:

[
     "218.200.188.34",
     "218.200.188.46",
     "218.200.188.34"
]

我怎樣才能用JS獲得它。

使用map返回一個新數組。 回調拆分:上的每個元素並返回該數組的第一個元素。

var out = arr.map(function (el) {
  return el.split(':')[0];
});

輸出

[ "218.200.188.34", "218.200.188.46", "218.200.188.34" ]

演示

 var p = ["218.200.188.34:13310", "218.200.188.46:11295", "218.200.188.34:17357" ] ; var result = p.map(function($this){ return $this.substring(0,$this.indexOf(":")); }) console.log(result);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

循環遍歷您的 JSON 並將您split的部分推入一個新數組:

for(var i =0; i < jsonIp.length; i++){
    jsonIp2.push(jsonIp[i].split(":")[0]);
}

https://jsfiddle.net/j4de2953/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM