deleting object elements from array using splice react native

vincent O

I am trying to add and delete an obect from an array, I have been able to figure out the add object part, but the deletion is not working. I have used filter() but it did nothing. Now I am using splice, it works but it deletes the first element, instead of the selected item. below is a sample code, and I have shown only the functions for better clarity.

        handleDelete(item) {

          this.setState(({ list}) => {
            const newList = [...list];
            newList.splice(item.key, 1);
            console.log('deleted', newList);
            return { list: newList };
          });
        }


        handleAdd() {
          const { firstname, lastname, email, phone} = this.state;
          const ID = uuid();
          const newItemObject = {
              key: ID,
              firstname: firstname,
              lastname: lastname,
              email: email,
              phone: phone,
              image: null,
          };

          this.setState(prevState => ({
            list: [...prevState.list, newItemObject]
          }));
        }

I would like to

Ori Drori

The item's key and index in the array are probably not the same. If the item is in the array, you can use Array.indexOf() to find it's index, and splice it out:

handleDelete(item) {
  this.setState(({ list }) => {
    const newList = [...list];
    const index = newList.indexOf(item);
    newList.splice(index, 1);

    return {
      list: newList
    };
  });
}

Or if you want to use Array.filter(), check if the key of of current element (o) is different from that of item:

handleDelete(item) {
  this.setState(({ list }) => ({
    list: list.filter(o => o.key !== item.key)
  }))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete object properties similiar like deleting array elements with splice?

From Dev

Removing an object's array elements using splice()

From Dev

splice multiple elements from an array using for loop

From Dev

Splice an object from an array in react usestate

From Javascript

Deleting array elements in JavaScript - delete vs splice

From Dev

React - Splice removing too many elements from state array

From Dev

Deleting object of a state array in React using id

From Dev

incremental/ decremental for-loops for removing elements from an array using splice

From Dev

Removing an object from array with splice() does not work as expected in React

From Dev

Array splice removes all elements from array

From Dev

Adding Elements in An Array Incrementally Using Splice Javascript

From Dev

Why is splice removing all the elements from an array?

From Dev

Deleting certain elements from numpy array using conditional checks

From Dev

Splice deleting first items from array of objects instead of index

From Dev

Array of text from a JSON object inserted into <Text> elements via forEach/map not rendering in React Native

From Dev

Rendering Elements from Object Array in React

From Dev

React-Native remove object from Array

From Dev

How to splice an object from array of objects?

From Dev

Splice Object from Array - undefined error

From Dev

Deleting from array via object

From Dev

Deleting an object from a array in javascript

From Dev

deleting an object from an array [java]

From Dev

Deleting object from state array

From Dev

JS - deleting object from array

From Dev

What's the advantage of using $splice (from immutability-helper) over filter to remove an item from an array in React?

From Dev

removing element from array using splice

From Dev

How to remove an item from array using Splice

From Javascript

Deleting from an object using JavaScript

From Dev

React Native: Passing specific object from array of object as props

Related Related

  1. 1

    Delete object properties similiar like deleting array elements with splice?

  2. 2

    Removing an object's array elements using splice()

  3. 3

    splice multiple elements from an array using for loop

  4. 4

    Splice an object from an array in react usestate

  5. 5

    Deleting array elements in JavaScript - delete vs splice

  6. 6

    React - Splice removing too many elements from state array

  7. 7

    Deleting object of a state array in React using id

  8. 8

    incremental/ decremental for-loops for removing elements from an array using splice

  9. 9

    Removing an object from array with splice() does not work as expected in React

  10. 10

    Array splice removes all elements from array

  11. 11

    Adding Elements in An Array Incrementally Using Splice Javascript

  12. 12

    Why is splice removing all the elements from an array?

  13. 13

    Deleting certain elements from numpy array using conditional checks

  14. 14

    Splice deleting first items from array of objects instead of index

  15. 15

    Array of text from a JSON object inserted into <Text> elements via forEach/map not rendering in React Native

  16. 16

    Rendering Elements from Object Array in React

  17. 17

    React-Native remove object from Array

  18. 18

    How to splice an object from array of objects?

  19. 19

    Splice Object from Array - undefined error

  20. 20

    Deleting from array via object

  21. 21

    Deleting an object from a array in javascript

  22. 22

    deleting an object from an array [java]

  23. 23

    Deleting object from state array

  24. 24

    JS - deleting object from array

  25. 25

    What's the advantage of using $splice (from immutability-helper) over filter to remove an item from an array in React?

  26. 26

    removing element from array using splice

  27. 27

    How to remove an item from array using Splice

  28. 28

    Deleting from an object using JavaScript

  29. 29

    React Native: Passing specific object from array of object as props

HotTag

Archive