JS indexOf array of objects and splice() not removing the correct object

javascript remove object from array by value
remove all occurrences of an element in array javascript
remove element from array javascript
typescript remove item from array of objects
javascript splice
remove last element from array javascript
delete element from array

I have an array of objects like so:

"followers": [
            "id": "1be87842-2f7f-4e3b-8fde-9a998feb3a01",
            "bug_id": "4ae2707b-07ef-4e07-95da-77855c67fece",
            "user_id": "e9e81aa2-4994-483d-a3a7-3b88491f1fda",
            "username": "texample1",
            "name": "Test Example1",
            "created_at": "2018-11-27 21:01:42",
            "updated_at": "2018-11-27 21:01:42",
            "deleted_at": null
            "id": "7bd1fa5f-4109-4beb-b53a-fb03a1d23536",
            "bug_id": "4ae2707b-07ef-4e07-95da-77855c67fece",
            "user_id": "e9e81aa2-4994-483d-a3a7-3b88491f1fda",
            "username": "texample1",
            "name": "Test Example2",
            "created_at": "2018-11-27 21:01:48",
            "updated_at": "2018-11-27 21:01:48",
            "deleted_at": null

and I am attempting to remove one object by it's index with the following code in my vuex store:

  let followersArray = state.bugs.find(b => b.id === follower.bug_id).followers
  let index = followersArray.indexOf(follower)
  followersArray.splice(index, 1)

I am passing an entire follower object through to this mutation, then finding the followers array on the bug object, finding the index and attempting to splice it from the full bug object's array of follower objects. This code removes another follower from the bug. The index logs as -1 and it should be 1. Anyone see what I'm missing here? If I could get the correct index, I would also add an if(index !== -1)) in there.

You could use findIndex() function and return the index of the follower based on his id :

      let index = followersArray.findIndex(i => i.id === follower.id);

Example :

let items = [{
  name: "aaa"
}, {
  name: "bbb"
}, {
  name: "ccc"
let c = {
  name: "ccc"
let index = items.findIndex(item => item.name === c.name)


Removing Elements from JavaScript Arrays, JavaScript's standard library doesn't provide a method for removing a specific Because there's no such built-in method, developers have to create their A common approach is to combine the indexOf() and splice() methods like this: Second, splice() is used to remove the array element at that index. Problem when you remove the item by value (i.e "mango") : It's remove all the items of array with the name of "mango" See in fiddle How to send current page number in Ajax request javascript , jquery , ajax , spring-mvc , datatables

When you run this code and it returns a -1:

let index = followersArray.indexOf(follower);

that means that the follower object is not contained in followersArray. The followersArray likely contains a copy of the follower object -- not a reference to the same object.

Even if the follower object has the same attributes and attribute values as the object in followersArray[1], the indexOf will return a -1 unless they are the same exact object.

Now if you just want to find an object in the array with a matching attribute value (such as id) then you could use map or findIndex to do so:

let index = followersArray.map(i => i.id).indexOf(follower.id);

Array.prototype.splice(), The splice() method changes the contents of an array by removing or replacing existing will behave as an adding function, adding as many element as item[n*] provided. If you do not specify any elements, splice() will only remove elements from the array. Standard built-in objects · Array; Properties. indexOf method in an object array in JavaScript To access the index of the object from the array of an object by having a value of an object, We are going to use a few of the methods. map()

If you print the result of followersArray in the console what does it show? If im correct find returns an array, even if it's only 1 element, try with this

let followersArray = state.bugs.find(b => b.id === follower.bug_id).followers[0]

9 Ways To Remove 🗑️ Elements From A JavaScript Array [Examples], How do you remove an object from an array of objects? Remove object from array of objects in Javascript. GitHub Gist: instantly share code, notes, and snippets.

Delete object from array in c++, . This is a simple example where the elements are integers. The elements to add to the array, beginning from start. If you do not specify any elements, splice() will only remove elements from the array. Return value. An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned. Description

How can I remove a specific item from an array?, How do you remove an object from an array of objects in C++? Announcement -> I am creating video tutorials of this website tutorials/articles/guides and publishing on my youtube channel at Java Guides - YouTube Channel.Subscribe to my youtube channel for daily useful videos updates.

JavaScript Array splice vs slice, like initialized with new[] is a buffer which pointer points at its first memory cell. Remove from array: Array.Filter vs Array.splice(Array.indexOf) JavaScript performance comparison. Test case created by on 2011-3-22. Preparation code