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

105
Visualizações
How to add new element to array which inside nested object by its path?

I want to add new element to array which inside nested object by using its path. I searched but didn't anything about that. For example my object is :

const sample = {
  enum: 4,
  key: "COMMON_TYPE",
  steps: [
    {
      title: "STEP ONE",
      description: "des1",
      instructions: [
        {
          icon: "step_power",
          label: {
            text: "METHOD_OVEN_DEFAULT_STEP_ONE_ICONTEXT",
            color: "A11111",
            location: "top",
          },
        },
      ],
    },
    {
      title: "STEP TWO",
      description: "des2",
      instructions: [
        {
          icon: "step_power",
          label: {
            text: "METHOD_OVEN_DEFAULT_STEP_TWO_ICONTEXT_ONE",
            color: "A11111",
            location: "top",
          },
        },   
      ],
    },
  ],
};

and I want to new element for this path 'steps.0.instructions' and second item should be copy of first index for that array. In onClick event I got path of key as 'steps.0.instructions'. Now I need a function which return newObject for given issue. My output should be like this:

const sample = {
  enum: 4,
  key: "COMMON_TYPE",
  steps: [
    {
      title: "STEP ONE",
      description: "des1",
      instructions: [
        {
          icon: "step_power",
          label: {
            text: "METHOD_OVEN_DEFAULT_STEP_ONE_ICONTEXT",
            color: "A11111",
            location: "top",
          },
       {
          icon: "step_power",
          label: {
            text: "METHOD_OVEN_DEFAULT_STEP_ONE_ICONTEXT",
            color: "A11111",
            location: "top",
          }
        },
      ],
    },
    {
      title: "STEP TWO",
      description: "des2",
      instructions: [
        {
          icon: "step_power",
          label: {
            text: "METHOD_OVEN_DEFAULT_STEP_TWO_ICONTEXT_ONE",
            color: "A11111",
            location: "top",
          },
        },   
      ],
    },
  ],
};

Thanks..

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

0

This does what you ask for.

I used JSON.parse(JSON.stringify(sample)) for a deep copy. You can do it in your preferred way.

const addToPath = (sample, path) => {
    const parts = path.split(".")
    const newSample = JSON.parse(JSON.stringify(sample))

    const instructionsArray = newSample[parts[0]][parts[1]][parts[2]]
    instructionsArray.push(instructionsArray[0])

    return newSample
}

And it will be called as follows:

const newSample = addToPath(sample, "steps.0.instructions")
about 4 years ago · Juan Pablo Isaza Relatório

0

use unshift function to add new item to the beginning of array. like

sample.steps.instructions.unshift( {
    icon: "step_power#2",
    label: {
      text: "METHOD_OVEN_DEFAULT_STEP_ONE_ICONTEXT",
      color: "A11111",
      location: "top",
    }
})
about 4 years ago · Juan Pablo Isaza Relatório

0

If you wanna copy an object, lookup what deep copy means.

Here are some helpful links:

  • stackoverflow: how to deep copy in js
  • MDN StructuredClone
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