javascript - Remove a key value from an object in react js - Stack Overflow

I have an object in the state ,this.state = {selectedValue: {}}Now,Here I am adding a property to this

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 ?

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 ?

Share Improve this question edited Jul 26, 2023 at 5:53 Pang 10.1k146 gold badges86 silver badges124 bronze badges asked Jun 17, 2019 at 11:11 ganesh kaspateganesh kaspate 2,69512 gold badges52 silver badges104 bronze badges 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; – SBylemans Commented Jun 17, 2019 at 11:15
Add a ment  | 

5 Answers 5

Reset to default 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.

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})

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.

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);

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
    });
   }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742357826a4428793.html

相关推荐

  • javascript - Remove a key value from an object in react js - Stack Overflow

    I have an object in the state ,this.state = {selectedValue: {}}Now,Here I am adding a property to this

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信