javascript - 怎么操作數(shù)組去分割冒號(hào)取出對(duì)應(yīng)的值。
問(wèn)題描述
['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ù)字并匹配在一起?不好意思,沒(méi)表述清楚;我需要獲取到冒號(hào)后面的數(shù)字,我整體了一下思路。
if(1001001) { var value == 95}
問(wèn)題解答
回答1:var obj = {};arr.forEach(function(item) { item = item.split(’:’) obj[item[0]] = item[1];});回答2:
按照你的if語(yǔ)句來(lái)說(shuō),如果你單純的想根據(jù)冒號(hào)前邊的id數(shù)字得到冒號(hào)后邊的值,那你可以用字符串的indexOf方法加substr方法來(lái)實(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));
頁(yè)面可以直接用
$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 - oslo_config2. 關(guān)于mysql聯(lián)合查詢一對(duì)多的顯示結(jié)果問(wèn)題3. 實(shí)現(xiàn)bing搜索工具urlAPI提交4. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)5. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。6. windows誤人子弟啊7. 冒昧問(wèn)一下,我這php代碼哪里出錯(cuò)了???8. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. mysql優(yōu)化 - MySQL如何為配置表建立索引?
