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.
You can use splice()
with indexOf()
var A = [10,54,65,12]; A.splice(A.indexOf(65), 1); console.log(A)
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);
}
Use splice
& indexOf
var a = [10,54,65,12]
var index = a.indexOf(65);
if (index > -1) {
a.splice(index, 1);
}
console.log(a)
Check this jsfiddle
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.