Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!