1

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

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

answered May 27, 2016 at 9:36

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.