I have to sort an array and want to get index so that I can sort another array on the basis of this index..
There are two array a and b I saved division of that array in third array that is sortDiv now I want index of small element so that I can sort a and b according to index..
Code is like
var a = [6, 7, 8, 9];
var b = [1, 2, 3, 4];
var sortA = [],
sortB = [],
sortDiv = [];
var div = a[0] / b[0];
for (var i = 0; i < a.length; i++) {
sortDiv.push(a[i] / b[i]);
}
var temp = sortDiv;
for (var i = 1; i < sortDiv.length; i++) {
var val = Math.min.apply(Math, temp);
var key = sortDiv.indexOf(val);
sortA.push(a[key]);
sortB.push(b[key]);
if (key > -1)
temp.splice(key, 1);
}
console.log(sortA + " " + sortB);
[9,8] for a and [4,3] for b..while I want a=[9,8,7,6] b=[1,2,3,4]
But splice is not a good option..I need a function that remove only element not an index..any idea please?
UPDATED As problem is solved but I want to know that Is it possible to remove element but not an index in array?