javascript - 怎么操作數(shù)組去分割冒號取出對應(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ù)組,取出冒號前后的數(shù)字并匹配在一起?不好意思,沒表述清楚;我需要獲取到冒號后面的數(shù)字,我整體了一下思路。
if(1001001) { var value == 95}
問題解答
回答1:var obj = {};arr.forEach(function(item) { item = item.split(’:’) obj[item[0]] = item[1];});回答2:
按照你的if語句來說,如果你單純的想根據(jù)冒號前邊的id數(shù)字得到冒號后邊的值,那你可以用字符串的indexOf方法加substr方法來實現(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ù)組遍歷,針對每一個元素做一次 : 分割,將分割后的元素放到一個新數(shù)組里。
回答4:我也不知道是不是應(yīng)該這樣搞啊,到時候取的話直接用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. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?2. mysql - 把一個表中的數(shù)據(jù)count更新到另一個表里?3. 請教使用PDO連接MSSQL數(shù)據(jù)庫插入是亂碼問題?4. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題5. visual-studio - Python OpenCV: 奇怪的自動補全問題6. linux - Ubuntu下編譯Vim8(+python)無數(shù)次編譯失敗7. node.js - nodejs開發(fā)中常用的連接mysql的庫8. mysql 查詢身份證號字段值有效的數(shù)據(jù)9. 視頻文件不能播放,怎么辦?10. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處
