V
V
Vadim2021-06-29 16:06:56
JavaScript
Vadim, 2021-06-29 16:06:56

How to change nested field?

Good afternoon!
There is such a data structure

const data = [
 {
  "id": 000,
  "content": "test0",
  "postedAt": 1581515871,
  "answers": [
    {
      "id": 001,
      "content": "test1",
      "postedAt": 1624960660,
      "answers": []
    }
  ]
}
]


And you need to get exactly the same structure at the output, but with a changed field postedAt
at all levels of nesting!

const filteredFirstMessage = useMemo(
        () => ([{
            ...data,
            postedAt: `${format(new Date(data.postedAt * 1000)}`,
        }]),
        [data],
    );

Such an entry works only for 1 nesting level, without taking into account in the arrayanswers

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2021-06-29
@Kozack

  1. Write a loop that iterates through some array of objects and passes each object to a conditional functionchangePostedAt
  2. The function changePostedAtchanges the field postedAt. It then finds the array in answersand for each object in that array calls changePostedAt.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question