I'm using SharePoint 2013 JSOM. My variable this.templates
contains a collection of templates with many different names. The only way to iterate through this collection is by using an Enumerator. I can only get the name of each template by using get_title()
.
I want to get the templates whose names respect the following rules:
- It contains "espace_projet"
- It contains the letter "v" followed by any number, once
Then, out of the templates i got, I want to get the one with the highest value that was after the "v".
I'm not used to JavaScript, this code works fine, but feels like overkill. How could I make it shorter/more readable?
ApplyTemplate() { var highestVersion = {number: -1, template: null}; var templateEnum = this.templates.getEnumerator(); while(templateEnum.moveNext()) { //Gets the element name in my collection var templateName = templateEnum.get_current().get_title(); if(templateName.includes('espace_projet')) { var version = templateName.match('v[0-9]+|v_[0-9]+'); if(version){ var versionNumber = version[0].match('[0-9]+'); if(versionNumber[0] > highestVersion.number) highestVersion = {number: versionNumber[0], template: templateEnum.get_current()}; } } } return highestVersion; }
v
? should such a string still be considered?\$\endgroup\$