3

I have an object with nested objects with the below structure, how can I dynamically add new items (newData) to the cost3 array?

I have tried that but the new data isn't being pushed, what am I doing wrong?

const [file, setFile] = useState({})

setFile(file=> ({
      ...file,
      [cost3]: {
          ...file.cost3,
          newData
      }
}))

File object:

{
      "info": {

      },
      "client": {

      },
      "costs": {
        "cost1": 1,
        "cost2": 5,
        "cost3": [
          {
            "a": "test",
            "b": "test",
            "c": "test",
          },
          {
            "d": "test",
            "e": "test",
            "f": "test",
          },
         //etc..       
        ],
        "cost4": [
          {
            "l": "test",
            "n": "test",
            "m": "test",
          },
        //etc..
        ]
      }
    }
1
  • What is [const3] in setFile, I don't see you define the const3 variable anywhere. Commented Oct 11, 2019 at 7:00

4 Answers 4

2

const file = {
  "info": {

  },
  "client": {

  },
  "costs": {
    "cost1": 1,
    "cost2": 5,
    "cost3": [{
        "a": "test",
        "b": "test",
        "c": "test",
      },
      {
        "d": "test",
        "e": "test",
        "f": "test",
      },
      //etc..       
    ],
    "cost4": [{
        "l": "test",
        "n": "test",
        "m": "test",
      },
      //etc..
    ]
  }
}

const newData = { x: 'I am new' }

console.log(
  {
    ...file,
    costs: {
      ...file.costs,
      cost3: [
        ...file.costs.cost3,
        newData
      ]
    }
  }
)

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

1 Comment

If you see how state is initialezed useState({}) you can conclude that ...file.costs.cost3, will throw a "const3 of undefined" error. I would suggest using some sort of get to make it less error prone.
2

Your code is incorrect.

Replace [cost3] with cost3 and {} with [], like so:

setFile(file=> ({
      ...file,
      costs: {
        ...file.costs,
        cost3: [
          ...file.costs.cost3,
          newData
        ]
      }
}))

1 Comment

Looks like OP is trying to set file.const.const3 but since file is initialized with {} trying to spread file.const.const3 will throw because const is initially undefined. Maybe better to use some sort of get
0

You can try it using concat.

cost3 = [];
const [file, setFile] = useState({});

setFile(file => ({
  cost3: file.cost3.concat(newData),
}));

1 Comment

I would suggest using concat instead of push because push will mutate the array.
-1

You can use .push() method to add item to object, for dynamic addition you can iterate through the array.

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.