0

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: "test@test.com",
    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.

Tholle
  • 108,070
  • 19
  • 198
  • 189

1 Answers1

0

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
    }
  }
}
Lelouch
  • 910
  • 6
  • 19