I have a scenario to create two new arrays(based on certain conditions) from an existing array. I have two different ways to get my result.
Use Jquery grep() twice to filter the original array.
var arrErrorRecs = [], arrWarningRecs = [];
arrErrorRecs = jQuery.grep(aSelectedRecs,function(oItem){
return oItem.Stat === "Error";
});
arrWarningRecs = jQuery.grep(aSelectedRecs,function(oItem){
return oItem.Stat === "Warning";
});
Use Jquery each() once and populate the two new arrays
jQuery.each(aSelectedRecs, function(){
if(this.MinPriceValidation === "Error"){
arrErrorRecs.push(this);
}else if(this.MinPriceValidation === "Warning"){
arrWarningRecs.push(this);
}
});
I have an understanding that in this case Jquery.each would be best as it is looping the array only once. Please advise which is the most effective way for better performance.
Since both code is O(n), I don’t think there is much problem with grep, but sure if the length of array is very huge, each would likely to be faster. But I believe not on the crazy difference though.