-1

I have js array and i use it as map it is dynamic ( in this example it is not ) So i dont know how many elements it will have .
i want to remove all the elements included the one found
This is the final view of the array after loop

movmet_sequence_history:[]
movmet_sequence_history["0"] = "A"
movmet_sequence_history["1"] = "B"
movmet_sequence_history["2"] = "C"
movmet_sequence_history["3"] = "D"
....
....
....
....
movmet_sequence_history["20"] = "Z"

if(movmet_sequence_history["2"] == "C")
{
  //HERE I WANT TO REMOVE THE ELEMENTS "2" ,"1" ,"0"
  //Only the movmet_sequence_history["3"] = "D" will remain
}

UPDATE: i dont what manually delete the right elements as i said the array is dynamic and i dont know how many elements will be up to the found one

user63898
  • 29,839
  • 85
  • 272
  • 514
  • why the tag *dictionary*? – Nina Scholz Jul 23 '16 at 11:55
  • Possible duplicate of [How to remove a property from a JavaScript object?](http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object) – GingerPlusPlus Jul 23 '16 at 12:14
  • @user63898 ... even with your last editing the expected processing behavior remains unclear. How exactly does the result of your provided example need to look like? And till now I can't recognize from the example arrays being (mis)used as maps. – Peter Seliger Jul 23 '16 at 13:43

3 Answers3

2

You can make them undefined.

movmet_sequence_history:[]
movmet_sequence_history["0"] = "A"
movmet_sequence_history["1"] = "B"
movmet_sequence_history["2"] = "C"
movmet_sequence_history["3"] = "D"

if(movmet_sequence_history["2"] == "C")
{
   movmet_sequence_history["0"] = undefined;
   movmet_sequence_history["1"] = undefined;
   movmet_sequence_history["2"] = undefined;
   //Only the movmet_sequence_history["3"] = "D" will remain
}
iuliu.net
  • 6,666
  • 6
  • 46
  • 69
1

Use delete operator.

The delete operator removes a property from an object.

var movement_sequence_history = ["A", "B", "C", "D" ];

if (movement_sequence_history["2"] == "C") {
    delete movement_sequence_history[0];
    delete movement_sequence_history[1];
    delete movement_sequence_history[2];
}

console.log(movement_sequence_history);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can use array.splice + array.fill. You can give index to array.splice and it will remove all items after that index and return them.

Now you just have to clear all values from original array and merge array returned from splice to it.

Sample

var movmet_sequence_history = []
movmet_sequence_history["0"] = "A"
movmet_sequence_history["1"] = "B"
movmet_sequence_history["2"] = "C"
movmet_sequence_history["3"] = "D"

function removeChar(str) {
  var index = movmet_sequence_history.indexOf(str)
  var _t = movmet_sequence_history.splice(index+1);
  movmet_sequence_history.fill(undefined)
  movmet_sequence_history = movmet_sequence_history.concat(_t)

  console.log(movmet_sequence_history)
}
<button onclick="removeChar('A')">Remove A</button>
<button onclick="removeChar('B')">Remove B</button>
<button onclick="removeChar('C')">Remove C</button>
<button onclick="removeChar('D')">Remove D</button>
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79