0

I have an object which has duplicate values so i used delete new_object[1] to delete the value but when I see this in console its showing undefined in object 0800

["293", undefined, "298", "297"]
Vikram
  • 3,171
  • 7
  • 37
  • 67

4 Answers4

4

You should use

arr.splice(index, 1);

delete only removes the element, but keeps the indexes. This question is similar in nature and provides more information.

Community
  • 1
  • 1
Oliver
  • 3,981
  • 2
  • 21
  • 35
1

I think you should use splice

a = ["1","2","3"];
a.splice(1,0)
console.log(a) //["1","3"]
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
1

var test = [1,2,3,4];

delete test[1];

now if you print the test variable, you will get

=> [ 1, , 3, 4 ]

that is why you have got undefined

like everyone here is answering, you should use splice

test.splice(1,1);

and now print the test variable will give you

=> [ 1, 3, 4, 5 ]

Community
  • 1
  • 1
Raaz
  • 1,669
  • 2
  • 24
  • 48
1

you need to use splice() in order to remove the value from the array. What you are doing is simply setting it to undefined.

var myArray = ['295', '296', '297', '298'];

// removes 1 element from index 2
var removed = myArray.splice(2, 1);
// myArray is ['295', '296', '298'];
// removed is ['297']

Reference from Array.splice

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

Carlos Morales
  • 5,676
  • 4
  • 34
  • 42