1

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().

  • 2
    Possible duplicate of [How do I remove a particular element from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) – JJJ Dec 14 '17 at 10:28

4 Answers4

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);
xs0
  • 2,914
  • 15
  • 25
4

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.

nicholaswmin
  • 17,508
  • 12
  • 71
  • 142
  • however filter will not remove from the original array (as the OP want) and will return another array. – Koushik Chatterjee Dec 14 '17 at 10:36
  • True - However I'd argue it's better to [avoid mutations](https://en.wikipedia.org/wiki/Functional_programming) where possible. – nicholaswmin Dec 14 '17 at 10:38
  • Agree, but that's all the question actually standing on – Koushik Chatterjee Dec 14 '17 at 10:40
  • 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. – nicholaswmin Dec 14 '17 at 10:41
  • Ii tried `.filter()` but i had some issues with it, that's why i asked about find :) – Eugen-Andrei Coliban Dec 14 '17 at 10:48
  • @Eugen-AndreiColiban Solve the issues of `filter()` instead of going for a more verbose and unnecessary approach, which is what you went with. Apart from that, the `index` approach you are going with cannot be easily composited/chained later on. Having implementation issues with a better solution is not an "excuse" to go with a worse solution. – nicholaswmin Dec 14 '17 at 10:48
  • 1
    @Eugen-AndreiColiban This is why you should [ask about your actual problem, not the supposed solution](http://xyproblem.info/). – JJJ Dec 14 '17 at 10:50
1

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);
}
Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148
0

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

Koushik Chatterjee
  • 3,738
  • 3
  • 15
  • 29