1

I have this object Geojson, when i do an "delete obj['features'][2]" , it left an "null" (look at the end), how can i delete this ? Or there is an another solution ?

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[4.23,48.17]},"properties":{"id":12,"subject":"Sujet essaicatogrie","duration":3,"dateStart":"2020-11-12T00:00:00+00:00","dateEnd":null,"gratis":true,"category":"Sport_walk","media":"5fadd4e225097948791983.jpg"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-0.71,47.02]},"properties":{"id":13,"subject":"Marche sur les terrils","duration":2,"dateStart":"2020-11-24T00:00:00+00:00","dateEnd":null,"gratis":true,"category":"Sport_walk","media":"5fb3d3d7061b4566505116.jpg"}},null, <------- this {"type":"Featur...

Francois T
  • 60
  • 7
  • delete work fine on object element, but you are removing an item from an array, use splice instead. Please check this answer https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array. – Popeye Feb 17 '21 at 22:42

2 Answers2

1

It's because you are trying use the notation to remove it from an object. In your example you are removing it from an array.

You should do something in the lines of:

obj['features'].splice(1, 2);
Jacob Hansen
  • 110
  • 9
0

You can use several methods to remove item(s) from an Array:

//1
someArray.shift(); // first element removed
//2
someArray = someArray.slice(1); // first element removed
//3
someArray.splice(0, 1); // first element removed
//4
someArray.pop(); // last element removed
//5
someArray = someArray.slice(0, a.length - 1); // last element removed
//6
someArray.length = someArray.length - 1; // last element removed

If you want to remove element at position x, use:

someArray.splice(x, 1);

in your case

obj['features'].splice(1, 2);
Muhammad Taseen
  • 369
  • 1
  • 17