Home > Software design >  Append new key value pair on Click in the existing mock data
Append new key value pair on Click in the existing mock data

Time:10-26

Hi I am working on a task where I need to change the value of the boolean first and next I need to append the new key value pair on click. I have tried to use . operator to append the new key value pair but it's resulting in the error. My mock data is arrasy of object I need to add new key value pair(isDev:false) after the isProcessing:true Could any one help me what I am doing wrong. Thanks in advance!

Mock Structure:

const mockItems = [
  {
    id: "1",
    itemType: "metal",
    doneBy: {
      id: "1",
      display: "Item Name"
    },
    catg: "A",
    createdDate: "01/01/2021",
    updatedBy: {
      id: "1",
      type: "M-A"
    },
    isProcessing: "true",
    subItems: [
      {
        id: "1",
        doneBy: {
          id: "1",
          display: "sub item name"
        },
        status: {
          type: "notapproved"
        },
        isProcessing: "true"
      }
    ]
  }
];

Code:

const [processingItem, setProcessingItem] = useState(mockItems);

const onClick = () => {
  setProcessingItem((prevState) => prevState.map((data) => (
  { ...data, isProcessing:false, data.isCreated=false})))
}

When I am adding the third argument my code is not working.

CodePudding user response:

In the object key pairs use key: value assigning format.

const [processingItem, setProcessingItem] = useState(mockItems);

const onClick = () => {
  setProcessingItem((prevState) => prevState.map((data) => (
  { ...data, isProcessing:false, isCreated: false})))
}

CodePudding user response:

Something like this.

{ ...data, isProcessing:false, isDev: data.isCreated===false})))
  • Related