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

176
Visualizações
Get an element from an array at an index a user specified

I want to build a function that takes two inputs:

  1. an array of arrays
  2. index number

and then returns an array inside the input array that it located at the input index.

The following is the code I got so far.

So, if a user calls the function with the following inputs, the expected result is [4,5,6] .

const input = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

const index = 1;

function grab(input, index) {
  var result = [];

  return result = input[index];
  console.log(result);
};

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

0

const input = [
       [1,2,3],
       [4,5,6],
       [7,8,9]
    ];
    
 const index = 1;

const result= grab(input, index);
console.log(result);

function grab(arr, index){
   return [...input[index]]; // return copy of selected array
   // to return not a copy -> return input[index];
};
about 4 years ago · Juan Pablo Isaza Relatório

0

function grab(input, index){
   var result = [];

   for(var i=0; i<input.length; i++){
          result.push(input[index][i]);
       }
       return result;
};

You could learn more about slicing 2-D arrays in javascript from this source: 1

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