0

I am trying to delete/remove an element from a date array. However, for some reason the last element is not being removed. I will give an example below:

I have a date array like arr[1900-12-20, 1898-12-20, 1900-12-20]; Please consider the elements in the array are dates and not strings.

Here is my code - My code take in an array of dates(input1[0]), one date attribute like '2014-09-30' (input1[1]), and returns an array of dates(output1)

var DobArr = input1[0];
var passedInDt = input1[1];
var prt_year = 0;
var ageInYears = 0;

//Get year for valuation date
valYear = passedInDt.getFullYear();

for (i=0; i<DobArr.length; i++){

    prt_year = DobArr[i].getFullYear();
    ageInYears = valYear - prt_year;

    if (ageInYears >= 110){
    removeByIndex(DobArr, i);
 }
}
output1 = DobArr;

function removeByIndex(arr, index) {
  arr.splice(index, 1);
}

So what the code does is checks if ageInYears >= 110, if it is then I would like to remove the element from array.

Let me give a side note here, an Oracle product will call this script, and input1[0] is an array of date elements. input1[0] in Oracle product looks like input1[0] = {1900-12-20}{1898-12-20}{1900-12-20}

After running my code, DobArr should be empty, instead it has a value of {1898-12-20} ... Why ?? All elements pass ageInYears >= 110, why do i have an element left ? i am confused. splice function should remove the elements completely from the array. Please Help and thanks in advance.

Thanks,

2
  • when you remove a element from DobArr, you don't remove the indice "i" by one. So I think some element will be forgotten Commented Nov 25, 2014 at 22:30
  • 1
    This is why you should not manipulate an array while looping over it. You can use the Array filter method to filter out elements and return a new array. Commented Nov 25, 2014 at 22:34

3 Answers 3

1

after removing an element by index - all indexes are shifting in 1 place... repeat that for several times and you will find that the loop index (in that case i) is moving by the old index count while the current index count already changes. so all you need to do is update i variable:

if (ageInYears >= 110)
{
    removeByIndex(DobArr, i); i--;
}
2
  • I was not aware of the indexes shifting by 1. Thank you for clarifying this to me. thanks a lot. i-- did solve my issue, and it looks like its working. But i am doing more testing currently. Commented Nov 25, 2014 at 23:01
  • by the way - this effect repeat all the time in all enviroments when you remove item from a collection by index.. keep in mind :) Commented Nov 25, 2014 at 23:08
0

First thing Search element which you want to delete:

var numArray = [2,3,5,6];
var index = numArray.indexOf(5);;

Now; use splice() method:

if (index > -1) {
 array.splice(index, 1);
}
-1

The splice method returns an array, so you would have to return arr.splice(index, 1) and reassign DobArr to removeByIndex(DobArr, i).

1
  • 1
    splice manipulates the actual array and returns the elements that were removed. So setting DobArr to the return is not what should be done Commented Nov 25, 2014 at 22:30

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.