javascript - 怎么操作數(shù)組去分割冒號(hào)取出對(duì)應(yīng)的值。
問題描述
['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]; }
我這樣編寫各位大神看看這樣有什么不妥?
相關(guān)文章:
1. 求大神支招,php怎么操作在一個(gè)html文件的<head>標(biāo)記內(nèi)添加內(nèi)容?2. 安裝了“PHP工具箱”,但只能以“游客”身份登錄3. 老師們php,插入數(shù)據(jù)庫mysql,都是空的,要怎么解決4. 跨類調(diào)用后,找不到方法5. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實(shí)現(xiàn)存在即更新應(yīng)該使用哪個(gè)標(biāo)簽?6. 致命錯(cuò)誤: Class ’appfacadeTest’ not found7. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)8. PHP類屬性聲明?9. 怎么php怎么通過數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。10. phpstady在win10上運(yùn)行
