1

I have initial values in react redux state which is basically fetched from an API. which is in the data

let data = {    
    item: [],
    name: "random",
    description: "hello",
    status: "true",
    asset: [],
    version: "0.043",
    }

now I want to replace the item array [] from the dat with the item array below

let item = [
{
    item: 
{
    name: "random",
    description: "General Description",
    status: "true",
    asset: [],
    version: "0.043",
   },
   quantity: "344"
   },
   {
   item: {
   name: "random",
   description: "General Description",
   status: "true",
   asset: [],
   version: "0.043"
   },
   quantity: "434"
   }
   ]

what I want to achieve is to update the react redux state and then post it to the Api

data = {
    item: [
    {
     item: {
        name: "random",
        descr: "General Descr",
        status: "true",
        asset: [],
        version: "0.043",
     },
      quantity: "344"
      },
      {
     item: {
        name: "random",
        descr: "General Descr",
        status: "true",
        asset: [],
        version: "0.043",
     },
      quantity: "344"
      }                     
     ],
    name: "random",
    description: "hello",
    status: "true",
    asset: [],
    version: "0.043",
}

1 Answer 1

1

You can use the spread operator for this to make a copy of the data and item. Which you can then use in your redux part of the code

data = { ...data, item: [...data.item, ...item] };
Sign up to request clarification or add additional context in comments.

2 Comments

If this was the correct answer, you can accept it as the correct answer to help others who might have the same issue in the future @MuhammadMisbahUlHaq
Is there any other way to accept the answer. I upvoted it but I don't have much reputation that's why its not accepting.

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.