2

So, I have an array, and I want to delete a specific element, through .find(). How should I use it for such purpose? I know that it should have a condition, let's say

if(element === selectedItem)
{
   Array.splice(index,1);
} 

but i do not know how to include this into .find().

1

4 Answers 4

7

Removing items from an Array is not find()'s purpose.

What you want is Array.filter().

From MDN Array.prototype.filter:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Here's an example:

var numbers = [1, 2, 3, 4, 5, 6]

var evenNumbers = numbers.filter(number => {
  // returning something that evaluates to `true` will
  // keep the item in the result Array
  return number % 2 === 0
})

console.log(evenNumbers)

Protip:

Don't instinctively try to composite the Array helper functions you already know into something that does what you want.

Instead, read the MDN Array docs, specifically the 'Methods' section from the sidebar, which already contains a lot of useful methods.

Sign up to request clarification or add additional context in comments.

7 Comments

however filter will not remove from the original array (as the OP want) and will return another array.
True - However I'd argue it's better to avoid mutations where possible.
Agree, but that's all the question actually standing on
There's no but - it's an answer that doesn't directly answer the question but still achieves the OP's purpose. Downvote if you don't agree and move on.
@Eugen-AndreiColiban This is why you should ask about your actual problem, not the supposed solution.
|
5

You can use findIndex instead of find:

var data = [
    { id: 1, name: 'Betty' },
    { id: 2, name: 'Mark' },
    { id: 3, name: 'Elizabeth' },
    { id: 4, name: 'Samuel' }    
];

var index = data.findIndex(x => x.name === 'Mark');
if (index >= 0)
    data.splice(index, 1);
    
console.log(data);

Comments

2

Let's suppose you have a condition function, like:

function condition(element) {
    return element === selectedItem;
}

Now, you can use find to find the value of the element like this:

var myItem = myArray.find(condition);

And then

myArray.splice(myArray.indexOf(myItem), 1);

However, if your array might have multiple matches to remove, then

var myItem;
while ((myItem = myArray.find(condition)) !== undefined) {
    myArray.splice(myArray.indexOf(myItem), 1);
}

Comments

0

To find the index, you need indexOf and not find, so that you can perform further operations on top of that.

3 Comments

There is a method find() which returns first element that satisfies the condition given. May be he needs that.
well, may be, but then he needs index of the element after that. well, that can be combined to findIndex
@Madhavan.V but as the OP posted element === selectedItem it seems like direct reference compare and should work with indexOf

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.