1

I have the following array that I'm trying to remove values from:

transformedColumns [ "Item No", "Item Type", "Item State", "Valid Design_text", "Currency Code", "Base Item_no", "Comclass Name" ]

I'm just trying to remove all values from the array with the splice function:

    var indexPos = [0, 1, 3, 4, 5, 6];
    for (var k = 0; k < indexPos.length; k++) {
        transformedColumns.splice(indexPos[k], 1);
    }

The result of transformedColumns after the execution of the code is:

transformedColumns [ "Item Type", "Valid Design_text", "Currency Code", "Comclass Name" ]

I guess the reason to the problem Is that when you do a splice, the whole order in the array are changed, so the indexes does not match in some way? If it's so, how can I remove value from an array in this kind of way?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bryan
  • 3,421
  • 8
  • 37
  • 77
  • 3
    When you splice, array is manipulated. This means index and length is changed. So it will not give you proper values. – Rajesh Aug 21 '17 at 12:53
  • 2
    you can you while loop for this – Rajesh Dan Aug 21 '17 at 12:54
  • 1
    Do you really need to mutate original array? Consider creating new array from `transformedColumn ` with `filter` function and reassigning the variable – Yury Tarabanko Aug 21 '17 at 12:55
  • 2
    *how can I remove value*, if you can show us the proper use-case, we would be able to help you with proper solution. But for now, you can do `transformedColumns = [];` – Rajesh Aug 21 '17 at 12:56

0 Answers0