Shariq Ansari Shariq Ansari - 1 year ago 65
Javascript Question

Change index of array values with keys

After running a function i am getting an array like this.

["1:s", "2:2", "0:f"]

but i want to convert this array like this


i mean index should be same as key.

Answer Source

You could just sort it with taking the index out of the string.

var array = ["1:s", "2:2", "0:f"];
array.sort(function (a, b) {
    return a.split(':')[0] - b.split(':')[0];