字符串单个替换
项目中遇到的问题,要把‘1111100’变成‘周一,周二,周三,周四,周五’。
vm.ruleList = [{ "alarmruleId": "222", "alarmruleName": "上学校门出入规则", "alarmruleinfo": { "deviceName": "校大门口", "startTime": "07:00", "endTime": "08:00", "flag": "0", "timeType": "0", "timeRepeat": "1111100", "timeDate": "2016-8-9 00:00:00" } }, { "alarmruleId": "2342", "alarmruleName": "上学校门出入规则", "alarmruleinfo": { "deviceName": "校大门口", "startTime": "07:00", "endTime": "08:00", "flag": "0", "timeType": "1", "timeRepeat": "0111100", "timeDate": "2016-8-9 00:00:00" } }] vm.arr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']; for (var i = 0; i < vm.ruleList.length; i++) { if (vm.ruleList[i].alarmruleinfo.timeType == 0) { vm.ruleList[i].alarmruleinfo.dateRepeat = []; for (var j = 0; j < 7; j++) { vm.ruleList[i].alarmruleinfo.timeRepeat.charAt(j) === '0' ? '' : vm.ruleList[i].alarmruleinfo.dateRepeat.push(vm.arr[j]); } vm.ruleList[i].alarmruleinfo.dateRepeat = vm.ruleList[i].alarmruleinfo.dateRepeat.join(','); } } $log.log(vm.ruleList)
耶!