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

118
Views
Get values from multidimensional arrays using javascript

Let suppose I have an array like this

Let data = [ [bob,male,90,pass][sam,male,70,pass][grace,female,75,pass][harry,male,20,fail] ]

and I want to extract all the names and store them in a seperate array then how can I do it ? Means output should be like

[bob,sam,grace,harry]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

1) You can use map here with destructuring

let data = [
  ["bob", "male", 90, "pass"],
  ["sam", "male", 70, "pass"],
  ["grace", "female", 75, "pass"],
  ["harry", "male", 20, "fail"],
];

const result = data.map(([name]) => name);
console.log(result);

2) Using map with index

let data = [
  ["bob", "male", 90, "pass"],
  ["sam", "male", 70, "pass"],
  ["grace", "female", 75, "pass"],
  ["harry", "male", 20, "fail"],
];

const result = data.map((arr) => arr[0]);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Using for loops and switch:

// data array
let data = [
  ['bob','male',90,true],
  ['sam','male',70,true],
  ['grace','female',75,true],
  ['harry','male',20,false]
];

// instantiate sorted arrays
let names = [];
let gender = [];
let age = [];
let pass = [];

// iterate on the array containing arrays
for (let x = 0; x < data.length; x++) {
  // iterate inside the arrays
  for (let y = 0; y < data[x].length; y++) {
    // curData store the data we just now are looking at
    // while iterating through all the data
    curData = data[x][y];
    // depending on y, save the data to the correct array
    switch (y) {
      case 0:
        names.push(curData);
        break;
      case 1:
        gender.push(curData);
        break;
      case 2:
        age.push(curData);
        break;
      case 3:
        pass.push(curData);
        break;
    }
  }
}

// print them out
console.log(names);
console.log(gender);
console.log(age);
console.log(pass);

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!