Skip to main content
Commonmark migration
Source Link

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }    
    })
    console.log(output)
}

Desired Output:

enter image description here

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

enter image description here

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }    
    })
    console.log(output)
}

Desired Output:

enter image description here

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

added 51 characters in body
Source Link

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

[01: "foo", 05: "bar", 100: "baz"]enter image description here

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

[01: "foo", 05: "bar", 100: "baz"]

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

enter image description here

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

added 4 characters in body
Source Link
Linny
  • 10.4k
  • 5
  • 37
  • 99

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}][{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

[01: "foo", 05: "bar", 100: "baz"][01: "foo", 05: "bar", 100: "baz"]

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

[01: "foo", 05: "bar", 100: "baz"]

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

I'm trying to create a sorted array based on the below input data:

Input:

[{"title": "100_baz"}, {"title": "01_foo"}, {"title": "05_bar"}]

function indexSort (input) {    
    let output = [];
    input.forEach(value => {    
        if (value.title && value.title.indexOf('_') !== -1) {
            let targetIndex = parseInt (value.title.split('_')[0]);
            let title = value.title.substring(value.title.indexOf('_') + 1, value.title.length); // extract the string next to '_'
            output[targetIndex] = title;
        }   
    })
    console.log(output)
}

Desired Output:

[01: "foo", 05: "bar", 100: "baz"]

I'm able to get the desired output using the above code snippet, however, I'm looking for a better way to achieve this in terms of good practice, or a concise and clear method in ES5 or ES6.

edited title
Link
Loading
added 194 characters in body
Source Link
Loading
Source Link
Loading