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.

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)

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);
}

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

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

Comments

Your Answer

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.