Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Replace array in nested object in Javascript

I have spent a good part of the day trying to replace arrays of an existing nested object but I can't figure out how to do it. This is my original object:

{
  "id": "a8df1653-238a-4f23-fe42-345c5d928b34",
  "webSections": {
    "id": "x58654a9-283b-4fa6-8466-3f7534783f8",
    "sections": [
      {
        "id": "92d7e428-4a5b-4f7e-bc7d-b761ca018922",
        "title": "Websites",
        "questions": [
          { 
            id: 'dee6e3a6-f207-f3db-921e-32a0b745557', 
            text: 'Website questions', 
            items: Array(11)
         }
        ]
      },
      {
        "id": "79e42d88-7dd0-4f70-b6b4-dea4b4a64ef3",
        "title": "Blogs",
        "questions": [
          ...
        ]
      },
      {
        "id": "439ded88-d7ed0-de70-b6b4-dea4b4a64e840",
        "title": "App questions",
        "questions": [
          ...
        ]
      }
    ]
}

I would like replace the question arrays in the original object or in a copy of it.

const newMenu = [
{id: '34bb96c7-1eda-4f10-8acf-e6486296f4dd', text: 'Website questions', items: Array(24)},
{id: '520c2d3f-6117-4f6a-904f-2477e3347472', text: 'Blog questions', item: Array(7)},
{id: '302b658a-9d8c-4f53-80f6-3f2275bfble', title: 'App questions', items: Array(14)}
]

I am trying to do this by its index but unfortunately it doesn't work.

 webSections.sections.forEach((item, index) => {
   return webSections.sections[index].questions, newMenu[index]);
 }

Does anyone see what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The value returned from the callback passed to forEach will not be used anywhere.

If you want to avoid mutating the original object and update questions, you can use Array.prototype.map and object spread syntax.

const object = {
  "id": "a8df1653-238a-4f23-fe42-345c5d928b34",
  "webSections": {
    "id": "x58654a9-283b-4fa6-8466-3f7534783f8",
    "sections": [
      {
        "id": "92d7e428-4a5b-4f7e-bc7d-b761ca018922",
        "title": "Websites",
        "questions": [
          { 
            id: 'dee6e3a6-f207-f3db-921e-32a0b745557', 
            ...

const updatedObject = {
 ...object,
 webSections: {
  ...object.webSections,
  sections: object.webSections.sections.map((section, index) => ({...section, questions: newMenu[index]}))
 }
}

If you just want to mutate the original object

object.webSections.sections.forEach((_, index) => {
 section.questions = newMenu[index]
})
about 4 years ago · Juan Pablo Isaza Relatório

0

    const newSections = myObj.webSections.sections.map((obj, index) => {
        const newQuestions = newItems[index];
        return {
            ...obj,
            questions: [newQuestions],
        };
    });

    console.log(newSections);

MyObj is the main object. This shall produce the new sections array you can combine it with your main object I suppose...

@Ramesh Reddy has the most thorough answer.

about 4 years ago · Juan Pablo Isaza Relatório

0

The simplest way if you don't care about mutation is:

myObject.webSections.sections.forEach((section, index) => {
  section.questions = newMenu[index].items;
})

You have used your 'forEach' with wrong syntax. Check MDN on how it's used.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda