0

I have following array of arrays showing employee name, id and department

var employees = [
["John","001","Sales"]
["Mike","002","Sales"]
["Adam","003","Eng"]
["Sam","004","Sales"]
["Emma","005","Eng"]
];

how can I get specific column from this array based on another column. For example, I would like to get ID numbers column for all 'Eng' department where result would be:

["003","005"]
Horzy
  • 23
  • 7

3 Answers3

1

At first you need to filter your array and then just get necessary values through map method:

var employees = [
  ["John","001","Sales"],
  ["Mike","002","Sales"],
  ["Adam","003","Eng"],
  ["Sam","004","Sales"],
  ["Emma","005","Eng"]
  ];    

  const result = employees.filter(f=> f.some(s=>s == 'Eng')).map(([a, b, c]) => b);

  console.log(result);
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

You can use map() on the array to filter through and get the data you want.

var employees = [
    ["John", "001", "Sales"],
    ["Mike", "002", "Sales"],
    ["Adam", "003", "Eng"],
    ["Sam", "004", "Sales"],
    ["Emma", "005", "Eng"]
];

function getColumnFor(data, dept) {
    const output = [];
    data.map(function (item) {
        if (item[2].toLowerCase() === dept.toLowerCase()) {
            output.push(item[1]);
        }
    });
    return output;
}

const result = getColumnFor(employees, "Eng");
console.log(result);
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

If your array in formate, you can do as below

var employees = [
["John","001","Sales"],
["Mike","002","Sales"],
["Adam","003","Eng"],
["Sam","004","Sales"],
["Emma","005","Eng"]
];


var codes = [];
for(var k in employees){
 if(employees[k][2] == 'Eng'){
    codes.push(employees[k][1]);
 }
  
}

console.log(codes);
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37