Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //function to get latest ABno;
- //function will return distinct latest version from an array of strings in the format 'ABxxx-vx',
- //where AB is the starting of the string,
- //xxx represents the number followed by AB,
- //-vx respresents the version and x at the end is the version number
- var ABNOLatest = function(ABarray){
- ABarray = ABarray.sort();
- //console.log(ABarray);
- var baseAB;
- var PrevAB = {};
- var maxver;
- var finalABList=[];
- ABarray.map(function(AB){
- var ABsplit = AB.split("-");
- if(ABsplit.length>1) maxver=parseInt(ABsplit[1].replace('V',''));
- else maxver=0;
- baseAB = ABsplit[0];
- //console.log(PrevAB,baseAB,maxver);
- //conditionals to check and update list of latest ABNo
- if(Object.keys(PrevAB).length === 0 && PrevAB.constructor === Object){ //checks whether object is empty or not
- //console.log("if loop");
- PrevAB.baseAB = baseAB;
- PrevAB.ver = maxver;
- PrevAB.AB=AB;
- finalABList.push(PrevAB.AB) //Initial push to finallist array
- //console.log(PrevAB);
- }
- else if(PrevAB.baseAB==baseAB){
- //console.log("else if loop ==> baseAB");
- finalABList[finalABList.length-1] = (PrevAB.ver<maxver? AB:PrevAB.AB); //sets Last ABNo based on version number
- //console.log(finalABList[finalABList.length-1]);
- PrevAB.ver= (PrevAB.ver<maxver? maxver:PrevAB.ver); //sets version number for Prev.ver
- //console.log("PrevAB.ve",PrevAB.ver);
- PrevAB.AB=AB;
- }
- else if (PrevAB.AB!=undefined && finalABList.indexOf(AB)==-1){//if current ABNo is not in the list
- //console.log("else if loop ==> finalABList");
- finalABList.push(AB);
- //console.log(AB);
- PrevAB.baseAB = baseAB;
- PrevAB.ver = maxver;
- PrevAB.AB=AB;
- }
- });
- console.log('finalABList',finalABList);
- return finalABList;
- }
- //Following is a sample array that you can use for test.
- var POar = ["AB1914411-V3", "AB2583130-V2", "AB2583130", "AB2017312-V3","AB2017312-V20", "AB2017312-V2", "AB1985437-V3", "AB1985437-V2"];
- ABNOLatest(POar);
Add Comment
Please, Sign In to add comment