["1001001:95", "1001002:100", "1001003:35", "1001004:37.5", "1001005:60", "1001006:100", "1001007:90", "1001008:140", "1001009:60", "1001010:90"]
怎麼去優雅的操作數組,取出冒號前後的數字並匹配在一起?
不好意思,沒表達清楚;
我需要取得到冒號後面的數字,我整體了一下思路。
if(1001001) {
var value == 95
}
世界只因有你2017-07-04 13:46:39
依照你的if語句來說,如果你單純的想根據冒號前邊的id數字得到冒號後邊的值,那你可以用字串的indexOf方法加substr方法來實現。
var array = ["1001001:95", "1001002:100", "1001003:35", "1001004:37.5", "1001005:60", "1001006:100", "1001007:90", "1001008:140", "1001009:60", "1001010:90"];
var number = "1001001";
var value = "";
for (var i = 0; i < array.length; i ++) {
if(array[i].indexOf(number)!= -1){
var index = array[i].indexOf(":");
value = array[i].substr(index + 1, array[i].length);
}
}
代言2017-07-04 13:46:39
我也不知道是不是應該這樣搞啊,到時候取的話直接用key取就可以了
var a = ["1001001:95", "1001002:100", "1001003:35", "1001004:37.5", "1001005:60", "1001006:100", "1001007:90", "1001008:140", "1001009:60", "1001010:90"];
function getResult(a,key){
var b = [];
var c = {};
for(var i = 0;i < a.length; i++){
b = a[i].split(":");
c[b[0]] = b[1];
}
return c[key];
}
console.log(getResult(a,1001001));
頁面可以直接用
$scope.spo_high = getResult(arr,data[0]);
某草草2017-07-04 13:46:39
data = data.split(':')
if(data[0] == 1001001) {
$scope.spo_low = data[1];
}else if(data[0] == 1001002){
$scope.spo_high = data[1];
}else if(data[0] == 1001003) {
$scope.temp_low = data[1];
}else if(data[0] == 1001004) {
$scope.temp_high = data[1];
}else if(data[0] == 1001005) {
$scope.plus_low = data[1];
}else if(data[0] == 1001006) {
$scope.plus_high = data[1];
}else if(data[0] == 1001007) {
$scope.sbp_low = data[1];
}else if(data[0] == 1001008) {
$scope.sbp_high = data[1];
}else if(data[0] == 1001009) {
$scope.dbp_low = data[1];
}else if(data[0] == 1001010) {
$scope.dbp_high = data[1];
}
我這樣寫各位大神看看這樣有什麼不妥?