Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I created a array with objects in javascript. that's what i got:

object[
   {position:1, value:23}, 
   {position:34, value:22 }, 
   {position:2, value:10},
   {position:35,value:9}.....
]

So i want to create a for loop, that deletes those objects who's destination (e.g. destination =(object1.position - object2.position) *-1) is lower than 18 to the previous objects.

for example: object[2].position is 1 position apart from object[0].position so object[2] is not needed anymore. The same for object[3] ... 35 - 34 = 1 / 1<18 / object[3] not needed.

that's what i wrote:

myfullarray = [
    {pos:1,value:23},
    {pos:34,value:22},
    {pos:2,value:10},
    (...)
]
myarray = [];
myarray[0] = {
    pos:myfullarray[0].pos,
    value:myfullarray[0].value
}

for(i=1;i<myarray.length;i++){
    for(d=i;d>0;d--){
      mydest = myfullarray[i].pos-myfullarray[d].pos;
      if(mydest<0){
        mydest *= -1
      }
      if(mydest<18){
      }else{

        myarray[myarray.length + 1] = {
          value:myfullarray[i].value,
          pos:myfullarray[i].pos
        };
      }
    }
 }

Can someone help me with this problem ?

share|improve this question
3  
What's the actual problem? What's your code doing that you don't want it to do, or what is it missing that you're trying to figure out? –  Herms Sep 24 '13 at 20:46
    
I see no splice(), pop(), or shift()... so, you're certainly not removing any items. –  canon Sep 24 '13 at 20:47
    
Well my problem is that my code didnt worked .. when i looked in my array i got 1884 objects in it ... and it should be around 3 or 15 –  mynameisgod Sep 24 '13 at 20:48
    
@canon yeah thats because i tried not to delete my objects in the array because that may cause many trouble with the for-loop. If you look to the code you may see that i call code(myarraylength) –  mynameisgod Sep 24 '13 at 20:49

2 Answers 2

up vote 0 down vote accepted

Here's a suggestion that avoids iterating quadratically. Create a new array. Then iterate through your full array, keeping track of the minimum and maximum positions so far, and if the current object has a position that's less than 18 from the minimum and maximum, add it to the new array.

EDIT: Here's a quick write-up of the code.

myFullArray = ...
myArray = [];
myArray[0] = myFullArray[0];
var min = myArray[0].pos;
var max = myArray[0].pos;
var current;
for (i = 1; i < myFullArray.length; i++) {
    current = myFullArray[i].pos;
    if (Math.abs(current - min) < 18) && Math.abs(current - max) < 18)
    {
        myArray.push(myFullArray[i]);
        if (current < min)
            min = current;
        else if (current > max)
            max = current;
    }
}
share|improve this answer
    
well that could work... Thanks :)) –  mynameisgod Sep 24 '13 at 20:59
    
It depends on what your exact problem is, actually. You may encounter an object whose position is greater than 18 away from a value that's between the min and max. If you care about that, this could fail. This works only if you consider distance from the lowest and highest positions you currently have. –  U-DON Sep 24 '13 at 21:04
    
Actually, sorry, mistake on my last comment. Should work for all values, because the bounds will be based on the first value of the list. Either the min or max will always be at most 17 away from the first position you added, so anything that's further than either bound will not be added. If the current considered value is not far enough away from the min, it will be far enough away from the max, and vice versa. –  U-DON Sep 24 '13 at 21:26

This should do the tick if you wanted to stick closer to your original algorithm.

myfullarray = [
    {position:1, value:23}, 
    {position:34, value:22 }, 
    {position:2, value:10},
    {position:35,value:9}
]
myarray = [];
myarray[0] = {
    pos:myfullarray[0].position,
    value:myfullarray[0].value
}
myfullarray.splice(0,1);

var mindest = 18;

for(i=0;i<myarray.length;i++){
    var myobject = myarray[i];
    for(d=i;d<myfullarray.length;d++){
      mydest = myobject.position-myfullarray[d].position;
      if(mydest<0){
        mydest *= -1
      }
      if(mydest<mindest){
          myfullarray.splice(d, 1);
      }
    }

    if (i < myfullarray.length) {
        myarray.push(myfullarray[i]);
        myfullarray.splice(i,1);
    }
}
share|improve this answer
    
thank you so much :D –  mynameisgod Sep 24 '13 at 21:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.