1 /**2 * like Map,but prev,curr,next and index would be given3 * @param {Function} cb callback,the Parameters ara Prev,curr,next and Index4 * @return {[type]} [description]5 */6array.prototype.bigmap=function(CB) {7 varcopy = This. Slice (0);8 varLen = This. Length;9 for(vari = 0; i < Len; i++) {Ten varCurr = This[i]; One varPrev = i==0?undefined: This[I-1]; A varNext = i==len?undefined: This[I+1]; - varRET =CB (CURR,PREV,NEXT,I); - if(ret) { thecopy[i]=ret; - } - } - returncopy; + } - + //Write a chestnut . A //[1,2,3,4,7,9,10,50] = ["1-4", "7", "9-10", "[]"] at - vararr = [1,2,3,4,7,9,10,50]; -arr = Arr.bigmap (function(curr,prev,next,i) { - //Console.log (curr+ "--" +prev+ "--" +next+ "--" +i ") - if(prev+1==Curr) { - varRET = "_"; in if(next-1!=Curr) { -RET + =Curr; to } + returnret; - } the}). Filter (function(obj) { * returnObj!= "_"; $}). toString (). Replace (/,_/g, "-"). Split (",");Panax Notoginseng -Console.log (arr)
JS Custom Array Map Bigmap