0

Currently trying to map over an array that has multiple nested objects. When I run the map function, it only returns the outer object.

enter image description here

const [moreDetails, setMoreDetails] = useState([]);
const [details, setDetails] = useState("");

const handleAddMoreDetails = () => {
  setMoreDetails((prevState) => [
    {
      ...prevState,
      details: details
    }
  ]);
  setDetails("");
};

return (
  <div className="App">
    <input
      type="text"
      value={details}
      onChange={(e) => setDetails(e.target.value)}
    />

    <button onClick={handleAddMoreDetails}>Add Detail</button>

    {moreDetails &&
      moreDetails.map((item, index) => <p key={index}>{item.details}</p>)}
  </div>
);

This is similar to what I am trying to accomplish. I would like for it to render all of them when added. I am thinking it may be the one I am trying to add a new detail

CodeSandbox

3
  • Do you want to iterate over objects as well ? Commented Aug 10, 2022 at 14:20
  • yes, i was trying to render the data from the objects Commented Aug 10, 2022 at 15:49
  • What is item in each iteration? Is it really an object as you claim? If it is an object, what are its keys? Are they what you expect? The screenshot of the structure of moreDetails doesn't seem to match what you think it is and you need to figure out why. Commented Aug 10, 2022 at 16:50

1 Answer 1

2

The problem isn't in the rendering, it's in the state update:

setMoreDetails((prevState) => [
  {
    ...prevState,
    details: details
  }
]);

If prevState was an array, for example:

[
  { details: 'first record' }
]

Then what happens after your state update? This update is creating an array with one object, which contains both the previous array and the new object. So the new result is:

[
  {
    0: {
      details: 'first record'
    },
    details: 'second record'
  }
]

Each update to the state will continue to create an array with one object, nesting the previous array inside that object.

Instead, create an array containing the elements of the previous array plus the one new object:

setMoreDetails((prevState) => [
  ...prevState,
  {
    details: details
  }
]);

So the new state would be:

[
  { details: 'first record' },
  { details: 'second record' }
]
Sign up to request clarification or add additional context in comments.

5 Comments

its a single array with nested objects. I tried flat(), but that didn't work
@arod1207: Can you perhaps replicate this in a runnable minimal reproducible example? It doesn't need to be a full React app. If the problem is simply mapping over a data structure then you can manually create the data structure and map over it, doing nothing more than logging a result to the console. That example can demonstrate what specifically isn't working as expected.
Just updated question with codesandbox link. You can see how my objects become nested in the array.
@arod1207: I've udpated the answer. The problem wasn't in mapping over the structure, the problem was in how you were creating/updating the structure.
Such a bonehead mistake on my part. You are exactly right. Thank you so much for the help and explanation

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.