I have a js object from my react app and want to delete a single data. The object is as follows

state = {
  data: []
}

const contactData = {
  Datas: {
    name: "william",
    email: "[email protected]",
    phoneNumber: "07123456",
    addressOne: "street1 ",
    addressTwo: "street2",
    postCode: "hd1 2rd ",
    country: "UK"
  }
};

const DateAdd = {
  ...contactData,
  id: new Date()
};
const immutate = {
  ...state,
  data: state.data.concat(DateAdd)
};

console.log(immutate);

I able to delete all the data with this method but not a single data

const deleteData = immutate.data.filter(own => own.id !== "generated date");

how would I able to delete like name or email only? appreciate for your helps.

1 Answers

0
Lelouch On

So you want to remove only one key from your Datas object right? Here is an example of removing only the email.

const { email, ...rest } = immutate.data.Datas

const deleteData = {
  ...immutate,
  data: {
    ...immutate.data,
    Datas: {
      ...immutate.data.Datas,
      ...rest
    }
  }
}