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:
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.