![](/img/trans.png)
[英]Angular how do I set ip address and port read from a json file as a service for other controllers
[英]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]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.