emotrend

Table filter

Feb 11th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. function solve(inputArr, command) {
  2. let header = inputArr.shift();
  3. let splCommand = command.split(' ');
  4.  
  5. switch(splCommand[0]) {
  6. case 'hide':
  7. hide(splCommand[1]);
  8. print();
  9. break;
  10. case 'sort':
  11. sort(splCommand[1]);
  12. print();
  13. break;
  14. case 'filter':
  15. filter(splCommand[1], splCommand[2]);
  16. break;
  17. }
  18.  
  19. function print() {
  20. console.log(header.join(' | '));
  21. inputArr.forEach(el => {
  22. console.log(el.join(' | '));
  23. })
  24. }
  25.  
  26. function hide(currHeader) {
  27. let currColIndex = header.indexOf(currHeader);
  28. inputArr.forEach(el => el.splice(currColIndex, 1));
  29. header.splice(currColIndex, 1);
  30. }
  31.  
  32. function sort(currHeader) {
  33. let currColIndex = header.indexOf(currHeader);
  34. inputArr.sort((a, b) => a[currColIndex].localeCompare(b[currColIndex]));
  35. }
  36.  
  37. function filter(currHeader, value) {
  38. let currColIndex = header.indexOf(currHeader);
  39. console.log(header.join(' | '));
  40. for (let el of inputArr) {
  41. for (let i = 0; i < el.length;i++) {
  42. if (el[i] === value && i === currColIndex) {
  43. console.log(el.join(' | '));
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. solve([['firstName', 'age', 'grade', 'course'],
  51. ['Peter', '25', '5.00', 'JS-Core'],
  52. ['Peter', '25', '5.00', 'JS-Core'],
  53. ['George', '34', '6.00', 'Tech'],
  54. ['Marry', '28', '5.49', 'Ruby']],
  55. 'filter firstName Marry');
Advertisement
Add Comment
Please, Sign In to add comment