成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 怎么操作數(shù)組去分割冒號(hào)取出對(duì)應(yīng)的值。

瀏覽:93日期:2024-03-04 08:08:31

問題描述

['1001001:95', '1001002:100', '1001003:35', '1001004:37.5', '1001005:60', '1001006:100', '1001007:90', '1001008:140', '1001009:60', '1001010:90']

怎么去優(yōu)雅的操作數(shù)組,取出冒號(hào)前后的數(shù)字并匹配在一起?不好意思,沒表述清楚;我需要獲取到冒號(hào)后面的數(shù)字,我整體了一下思路。

if(1001001) { var value == 95}

問題解答

回答1:

var obj = {};arr.forEach(function(item) { item = item.split(’:’) obj[item[0]] = item[1];});回答2:

按照你的if語句來說,如果你單純的想根據(jù)冒號(hào)前邊的id數(shù)字得到冒號(hào)后邊的值,那你可以用字符串的indexOf方法加substr方法來實(shí)現(xiàn)。

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); }}回答3:

數(shù)組遍歷,針對(duì)每一個(gè)元素做一次 : 分割,將分割后的元素放到一個(gè)新數(shù)組里。

回答4:

我也不知道是不是應(yīng)該這樣搞啊,到時(shí)候取的話直接用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]);回答5:

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回答6:

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]; }

我這樣編寫各位大神看看這樣有什么不妥?

標(biāo)簽: JavaScript
相關(guān)文章: