I'm using SharePoint 2013 JSOM, my. My variable this.templates
contains a collection of templates with many different names
The. The only way to iterate through this collection is by using an Enumerator
I. 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 :
- itIt contains "espace_projet"
- itIt 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 JavascriptJavaScript, the belowthis code works fine, but feels like an overkill, how. 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;
}