elena1234

Filter Employees - JavaScript - from string array into JSON objects

Nov 28th, 2021
864
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Write a program that filters the employees of your company. You should  print the result in a specific format. You will receive 2 parameters (data, criteria). You should parse the input, find all employees that fullfil the citeria and print them.
  2. Input
  3. You will receive a string with all the employees, and a criteria by witch you should sort the employees. If the criteria is "all" print all the employees in the given format.
  4.  
  5.  
  6. function solve(data, criteria) {
  7.     let employees = JSON.parse(data);
  8.     let [key, value] = criteria.split('-');
  9.  
  10.     employees.filter(employee => employee[key] == value || key == 'all')
  11.         .forEach((employee, i) => {
  12.             console.log(`${i}. ${employee.first_name} ${employee.last_name} - ${employee.email}`);
  13.         });
  14. }
  15.  
  16. solve(`[{
  17.     "id": "1",
  18.     "first_name": "Ardine",
  19.     "last_name": "Bassam",
  20.     "email": "abassam0@cnn.com",
  21.     "gender": "Female"
  22.   }, {
  23.     "id": "2",
  24.     "first_name": "Kizzee",
  25.     "last_name": "Jost",
  26.     "email": "kjost1@forbes.com",
  27.     "gender": "Female"
  28.   },  
  29. {
  30.     "id": "3",
  31.     "first_name": "Evanne",
  32.     "last_name": "Maldin",
  33.     "email": "emaldin2@hostgator.com",
  34.     "gender": "Male"
  35.   }]`,
  36.     'gender-Female'
  37. )
RAW Paste Data