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

142
Visualizações
How to adjust only part of an array, but other parts keep the same?

this is my array

    export const someList = {
      something1: 'some string 1',
      someArr: [
        {
          item: 'item 1',
          },
        },
        {
          item: 'item 2',
          },
        },
        {
          item: 'item 3',
          },
        ],
     something2: {
        some: 'some string 2'
      },
    };

Ok, so what I am trying to achieve is to construct a new array that would look like this:

export const newList = {
  something1: 'some string 1',
    someArr: [
            {
              item: 'item 1',
              },
            },
            {
              item: 'item 2',
              },
            ],
         something2: {
            some: 'some string 2'
          },
        };

So everything remains the same, except that newList takes only first two items from someArr.

I have tried this and it works correctly, but I don't know how to keep "outer" parts in new array (something1 and something2).

const newList = someList.someArr.slice(0, 2)

How can I construct a new array, keeping what I want, but still slicing the items from deeper nested array?

Thanks.

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

0

Ok after comments I re-read question I think you are trying to make a new OBJECT not a new ARRAY (curly braces are an object).

So with your code to do an OBJECT deep copy, then change the inside array, it will look like:

export const someList = {
  something1: 'some string 1',
  someArr: [
    {
      item: 'item 1',
      },
    },
    {
      item: 'item 2',
      },
    },
    {
      item: 'item 3',
      },
    ],
 something2: {
    some: 'some string 2'
  },
};
let newList=JSON.parse(JSON.stringify(someList));
newList.someArr=newList.someArr.slice(0,2);
about 4 years ago · Juan Pablo Isaza Relatório

0

What Event_Horizon answered is correct.

Another way of creating new Object:

const someList = {
      something1: 'some string 1',
      someArr: [
        {
          item: 'item 1',
        },
        {
          item: 'item 2',
        },
        {
          item: 'item 3'
        }
     ],
     something2: {
        some: 'some string 2'
     },
};

let newList={...someList,someArr:someList.someArr.slice(0,2)}
    
console.log(newList)

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