1

I have starting data with 2 entries:

var startdata = [{id: 1454498069524, lender: "examplelender", deptor: 'exampledeptor', amount: 10, text: "exampletext"}, {id: 1454498069511, lender: "examplelender2", deptor: 'exampledeptor2', amount: 102, text: "exampletext2"} ];

and I save this data in React State.

Then I try to delete from this State with a key value, but can't figure out how.

I tried:

handleDeptDelete: function(deptID) {
      var depts = this.state.data;
      var newDepts = depts;

      // delete newDepts[1454498069524]; // doesn't delete
      // delete {id: depts.deptID}; // doesn't delete
      newDepts.splice({id: deptID}, 1); // deletes always the first entry no matter of the ID

      this.setState({data: newDepts});
    },

I have console logs to show that the ID is correct and with the most recent try (the one not commented) I manage to delete something (always the first one in the data), so the update is working also fine.

How do I delete the second one? Making this:

{id: 1454498069524, lender: "examplelender", deptor: 'exampledeptor', amount: 10, text: "exampletext"}, {id: 1454498069511, lender: "examplelender2", deptor: 'exampledeptor2', amount: 102, text: "exampletext2"}

Into this:

{id: 1454498069524, lender: "examplelender", deptor: 'exampledeptor', amount: 10, text: "exampletext"}

I am able to get all the data of the entry, not just ID, but I thought it should be enough.

I don't have the data on server so I can't use This SO solution. Other ones I looked through are this and this with no help.

Community
  • 1
  • 1
Jovee
  • 23
  • 5
  • If you know the `index` of the element to delete you can use `newDepts.splice(index)` – leo.fcx Feb 03 '16 at 14:43
  • @leo.fcx Yes, I don't know the index. But I got the answer from the dublicate question that felix-kling linked. Just thought there would be more stylish way, but I guess I am naive :) – Jovee Feb 04 '16 at 07:37

0 Answers0