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

116
Visualizações
JS create new array from certain item

I have 2 dimensional array myArray:

[
  [ '567576', 'John', 'Doe' ],
  [ '098897', 'John', 'Doe' ],
  [ '543539', 'John', 'Doe' ],
  [ '234235', 'John', 'Doe' ],
  [ '345348', 'John', 'Doe' ],
  [ '432574', 'John', 'Doe' ]
]

Is it possible to create a new array from myArray starting from a certain first value? For example create a new array starting from id 543539. Anything above the array containing 543539 will not be added.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You can make use of findIndex() which returns the index of the first item that matches the condition. Combine it with slice() to cut your array at the specific position:

const myArray = [
  ['567576', 'John', 'Doe'],
  ['098897', 'John', 'Doe'],
  ['543539', 'John', 'Doe'],
  ['234235', 'John', 'Doe'],
  ['345348', 'John', 'Doe'],
  ['432574', 'John', 'Doe']
]

console.log(myArray.slice(myArray.findIndex(item => item[0] === "543539")));

about 4 years ago · Santiago Gelvez Relatório

0

const arr=[
  [ '567576', 'John', 'Doe' ],
  [ '098897', 'John', 'Doe' ],
  [ '543539', 'John', 'Doe' ],
  [ '234235', 'John', 'Doe' ],
  [ '345348', 'John', 'Doe' ],
  [ '432574', 'John', 'Doe' ]
];

const res=arr.slice(arr.findIndex(a=>a[0]=='543539'));

console.log(res);

about 4 years ago · Santiago Gelvez Relatório

0

Sure, assuming all the internal arrays are structured the same way, you can do something like let newArray = myArray.filter(innerArray => Number(innerArray[0]) > 543539)

Edit: This is assuming you want all subarrays where the first item is a number larger than 54539, as opposed to finding all of the subarrays that physically follow the first occurrence of a subarray with 54539 as the first value

about 4 years ago · Santiago Gelvez 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