Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda