javascript - How to do two validation checks on an array without looping two times -
i have got array of js objects on want few validation checks. should following checks priority in order given.
- check if value correct. (obj.value == 'valid')
- only 1 object should have value. other should empty.
- value of objects empty invalid option.
based on these checks, invalid objects should have msg property gets added proper message. have created function this. however, doesn't seem solution. looping through array 2 times correct msg. there way?
please note that, should compatible ie8. array methods every not work.
please see jsfiddle
var arr = [{ value: 'valid' }, { value: '' }, { value: 'someinvalid value' }]; function validate() { var counter = 0, morethanoneitemselected = false, noitemselected = true; (var = 0; < arr.length; i++) { arr[i].msg = ''; if (arr[i].value.length > 0) { if (arr[i].value != 'valid') { arr[i].isvalid = false; arr[i].msg = 'this invalid value'; } counter++; noitemselected = false; } if (!arr[i].msg) { arr[i].isvalid = true; } } if (counter > 1) { morethanoneitemselected = true; } if (noitemselected) { (var k = 0; k < arr.length; k++) { arr[k].isvalid = false; arr[i].msg = 'need set atleast 1 item'; } } else { (var j = 0; j < arr.length; j++) { var curritem = arr[j]; /*invalid item*/ if (curritem.isvalid && morethanoneitemselected && curritem.value.length > 0) { curritem.isvalid = false; curritem.msg = 'add 1 item'; } } } } validate(); console.log(arr);
thanks help
as have action depends on scanning items, , requires updating scanned items based on final result, there no way in 1 loop, best option save references of valid items, loop on them if more 1 item selected.
best case scenario: have 1 valid value (you wouldn't loop array again).
worst case scenario: items in array valid (you loop valid values array, same looping same array again).
update
i re-read post time, , got is:
- valid:
- only 1 item having value == 'valid', others should empty.
- invalid:
- all empty.
- more 1 none empty item either valid or invalid.
if above true, have perform revisit after scanning items:
function validate(arr){ var firstvaliditem = null; var containsinvalid = false; var mightrevisit = []; for(var x=0;x<arr.length;x++){ var item = arr[x]; switch(item.value){ case 'valid': // 1 valid item allowed. if(firstvaliditem){ item.msg = 'add 1 item'; item.isvalid = false; }else{ firstvaliditem = item; item.isvalid = true; } break; case '': //empty considered valid @ point item.isvalid = true; mightrevisit.push(item); break; default: //this invalid regardless of others item.isvalid = false; item.msg = 'this invalid value'; containsinvalid = true; break; } } //if no valid item found, empty items should become invalid if(!firstvaliditem){ for(var x=0;x<mightrevisit.length;x++){ mightrevisit[x].isvalid = false; mightrevisit[x].msg = 'need set atleast 1 item'; } }else if(containsinvalid){ //any invalid items makes valid item invalid. firstvaliditem.isvalid = false; firstvaliditem.msg = 'add 1 item'; } }
best case scenario: [{value: 'valid'},{value: ''},{value: ''}];
you'll not loop mightrevisit
array.
worst case scenario: [{value: ''},{value:''},{value: ''}]
you'll loop mightrevisit
array equal second loop of original array.
Comments
Post a Comment