Domanda di colloquio di Amazon

filter table using vanilla js

Risposte di colloquio

Anonimo

12 nov 2017

Assuming it's a simple "equal" filter and the table is a 2-dimensional array like this- var table = [ ['name', 'occupation', 'age'], ['Roy1', 'Software Engineer', '29'], ['Roy2', 'Software Engineer', '26'] ]; I'd go with - function filterTable(table, param, value) { var paramIndex = table[0].indexOf(param); return table.reduce(function (acc, row) { if (row[paramIndex] === value) { acc.push(row); } return acc; }, []) } And then, for example for the input - filterTable(table, 'age', '29') I'd get back - [['Roy1', 'Software Engineer', '29']]

Anonimo

19 lug 2020

var table = [ ['name', 'occupation', 'age'], ['Roy1', 'Software Engineer', '28'], ['Roy2', 'Software Engineer', '29'] ]; function filterTable(table, param, value) { var paramIndex = table[0].indexOf(param); return table.filter(item=>item[paramIndex]===value) } var res=filterTable(table, 'age', '29') console.log(abc)

Anonimo

2 ago 2020

var table = [ ['name', 'occupation', 'age'], ['Roy1', 'Software Engineer', '29'], ['Roy2', 'Software Engineer', '26'] ]; let filterTable = (table, field, val) => { const col = table[0].indexOf(field); return table.filter(row => row[col] === val)[0]; } let res = filterTable(table, 'name', 'Roy2'); console.log(res);