Skip to main content
deleted 24 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Sorting a JavascriptJavaScript array with a Regex

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;
}

Sorting a Javascript array with a Regex

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, the below code works fine, but feels like an 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;
}

Sorting a JavaScript array with a Regex

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;
}
Source Link
Treycos
  • 340
  • 1
  • 10

Sorting a Javascript array with a Regex

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, the below code works fine, but feels like an 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;
}