I want to delete an specific element in an array. I have this for example:
A = [10,54,65,12] and I want to delete the '65' number. How can I do this?
I try the pop() function but this deletes my last number.
asked May 27, 2016 at 9:32
PRVS
1,7006 gold badges40 silver badges75 bronze badges
4 Answers 4
You can use splice() with indexOf()
var A = [10,54,65,12];
A.splice(A.indexOf(65), 1);
console.log(A)
answered May 27, 2016 at 9:34
Nenad Vracar
122k16 gold badges160 silver badges184 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You need to find the index of element using .indexof() and then remove it using .splice():
var index = A.indexOf(65);
if (index > -1) {
A.splice(index, 1);
}
answered May 27, 2016 at 9:34
Milind Anantwar
82.3k26 gold badges97 silver badges128 bronze badges
Comments
You can use lodash library for doing this. '_.remove'
var A = [10,54,65,12];
_.remove(A, 65);
console.log(A)
// [10,54,12]
for more, check this https://lodash.com/docs
answered May 27, 2016 at 9:44
yugantar kumar
2,0501 gold badge25 silver badges34 bronze badges
Comments
lang-js