["1001001:95", "1001002:100", "1001003:35", "1001004:37.5", "1001005:60", "1001006:100", "1001007:90", "1001008:140", "1001009:60", "1001010:90"]
How to operate an array elegantly, take out the numbers before and after the colon and match them together?
Sorry, I didn’t express it clearly;
I need to get the number after the colon, so I thought about it.
if(1001001) {
var value == 95
}
欧阳克2017-07-04 13:46:39
var obj = {};
arr.forEach(function(item) {
item = item.split(':')
obj[item[0]] = item[1];
});
世界只因有你2017-07-04 13:46:39
According to your if statement, if you simply want to get the value after the colon based on the id number before the colon, then you can use the indexOf method of the string plus the substr method to achieve this.
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
Array traversal, do one split for each element, and put the split elements into a new array.
代言2017-07-04 13:46:39
I don’t know if I should do it this way. If I want to withdraw it at that time, I can just use the key to withdraw it
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));
The page can be used directly
$scope.spo_high = getResult(arr,data[0]);
大家讲道理2017-07-04 13:46:39
const array = ["1001001:95", "1001002:100", "1001003:35", "1001004:37.5", "1001005:60", "1001006:100", "1001007:90", "1001008:140", "1001009:60", "1001010:90"];
let result = array.reduce((a,b)=>{
let [key,value] = b.split(':');
a[key] = value;
return a;
},{});
console.log(result['1001001']);// 95
某草草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];
}
I wrote it like this. Can you guys see what’s wrong with this?