0

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);
I got [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?

5
  • Can you please clarify what exactly you want . Commented Feb 25, 2016 at 5:20
  • @HeemanshuBhalla Yes, I would comment the same thing. Also remove all the unnecessary code to be more specific. Commented Feb 25, 2016 at 5:33
  • @HeemanshuBhalla I have updated my question please check it out Commented Feb 25, 2016 at 5:36
  • 1
    check out this stackoverflow link stackoverflow.com/questions/17387571/… Commented Feb 25, 2016 at 6:22
  • That helped thanx @RajeshJangid Commented Feb 25, 2016 at 6:27

2 Answers 2

1

Try not removing element just replacing it with maximum element +1 and it will work fine here is the updated code

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]);
}
console.log(sortDiv);

var temp = sortDiv;
var max = Math.max.apply(Math, temp);  // here we find the maximum
max += 1;
for (var i = 1; i <= sortDiv.length; i++) {
  var val = Math.min.apply(Math, temp);
  console.log(val);
  var key = sortDiv.indexOf(val);
  sortA.push(a[key]);
  sortB.push(b[key]);
  temp[key] = max;   // here we update the minimum with maximum+1
}
console.log(sortA + " " + sortB);
Sign up to request clarification or add additional context in comments.

3 Comments

Isn't it possible with splice? can we remove element from array not an index ?
using splice changes indexes of elements after one iteration hence we cannot use splice here
don't know exactly but i think it is not possible to remove element with keeping the index undefined
0

If the question is about using splice to remove an item based on the value and not the index, get the index, then use splice:

var a = ['a', 'b', 'c'];
a.splice(a.indexOf('b'), 1);
console.log(a); // ["a", "c"]

3 Comments

I have used splice but it doesnot give an appropriate result
'appropriate' or 'correct'? Are you trying to remove your use of slice, correct your use of splice, or something else?
yeah I meant correct..I didnot get my answer as I want..I do not know splice is correct method to use or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.