1

I have an object in the state ,

this.state = {
   selectedValue: {}
}

Now,Here I am adding a property to this by object in the following way

if (e.currentTarget.checked) {
      this.setState({
        selectedType: {
          ...this.state.selectedType,
          [resumeId]: type
        }
      })

Now, In else part I have to remove the property with the matching resumeId.

Or Do I need to create an array of objects ? I am kind of confused here.

Can any one help me with this ?

1
  • What is the data you're trying to capture? It's not clear from your question. You can delete a key from an object by using delete object.key; Commented Jun 17, 2019 at 11:15

5 Answers 5

2

I know this is an old question but I think it deserves an answer so here is what you should do:

setState(current => {
    if (current) {
       const { yourRemoveKey, ...rest } = current;
       return rest;
    } else {
       return current;
    }
});

I think this is the best way.

remember that you might not need the if(current) part.

Sign up to request clarification or add additional context in comments.

Comments

1

The best way to do this is add a prefix to your resumId:

if (e.currentTarget.checked) {
      this.setState({
        selectedType: {
          ...this.state.selectedType,
          [`resume-${resumeId}`]: type
        }
      })

Now, you have a way to identify your resumeId. Then loop through your selectedType state and remove resumeId. You can do it as the following:

let selectedType = this.state.selectedType;
for (let key in selectedType) {
  if (key.indexOf('resume') !== -1) {
    delete selectedType[key]
  }
}
this.setState({selectedType})

8 Comments

Do I need to create an array of objects ? Because this way it is not adding the key values ?
You don't need to do that, I added code which demonstrate you how to do that.
Sorry no I did not mean that. I was just saying as you said to identify that
Actually it is removing all the values in the removing part
The resume prefix just help you to identify that all resumeID with resume prefix, remove it. Or if there isn't another property apart from your resumeID into your selectedType, just loop through the key then remove it.
|
1

if (e.currentTarget.checked) {
      this.setState({
        selectedType: {
          ...this.state.selectedType,
          [resumeId]: type
        }
      }) else {
        const selectedType = {
          ...this.state.selectedType
        }
        delete selectedType[resumeId];
        this.setState({
          selectedType
        });
      }

You can delete the resumeId from the object iself.

2 Comments

First one is not getting added If I do console log then.. I am not able to see the first one
When I click second time then the first one gets added .. is it because of setState ?
1

Use Object destructuring to acheive this cleanly:

if (e.currentTarget.checked) {
      this.setState({
        selectedType: {
          ...this.state.selectedType,
          [resumeId]: type
        }
      })
} else {
    // Destructure the resumeId and rest properties
    const { resumeId, ...rest} = this.setState.selectedType;

    // Only assign the rest properties now
    this.setState({ selectedType: ...rest });
}

Update:

To check if all values are same:

const data = { "a": "11", "b": "11", "c":"12", "d" : "11" };

const objectValues = Object.values(data);

// Check first value with every value
const isEveryValueSame = objectValues.every(x => x === objectValues[0]);

console.log(isEveryValueSame);

5 Comments

You should be using this.setState({ selectedType: ...rest }).
@Dan sorry my bad, not familiar with React
Hi if I have an object like , { "a": "11", "b": "11", "c":"12", "d" : "11" } as the question object , so is there any through which I will come to know if the values are same for all the keys ? i mean If values are same for all the keys will return true or else false for the same object ?
@ganeshkaspate Yes we can do that
@KunalMukherjee could you please help me with that ?
0

To remove a key-value pair from an object in React, you can create a copy of the object using the spread operator and then delete the desired key from the copy. Finally, update the state with the new object. Here's how you can do it:

Let's assume you want to remove the key-value pair with the key resumeId from the selectedType object:

if (e.currentTarget.checked) {
  // Create a copy of the selectedType object
  const updatedSelectedType = { ...this.state.selectedType };

   // Replace the property with undefined or use delete keyword
  const { resumeId } = someData; // Assuming you have the resumeId
  updatedSelectedType[resumeId] = undefined; // or delete 
 updatedSelectedType[resumeId];

  // Update the state with the modified object
   this.setState({
      selectedType: updatedSelectedType
    });
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.