Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. function getData() {
  2. const allPeople = JSON.parse(document.querySelector('textarea').value); //all data as objects
  3. const sortCondition = allPeople.pop();
  4.  
  5. let peopleIn = []; //as obj
  6. let peopleOut = []; //as obj
  7. let blacklist = []; //as obj
  8.  
  9. allPeople.forEach(person => {
  10. if (person.action === 'peopleIn') {
  11. if (!blacklist.find(p => p.firstName === person.firstName && p.lastName === person.lastName)) {
  12. peopleIn.push(person);
  13. }
  14. } else if (person.action === 'peopleOut') {
  15. const personFound = peopleIn.find(p => p.firstName === person.firstName && p.lastName === person.lastName);
  16.  
  17. if (personFound) {
  18. const index = peopleIn.indexOf(personFound);
  19. peopleIn.splice(index, 1);
  20. peopleOut.push(person);
  21. }
  22. } else if (person.action === 'blacklist'){ //blacklist
  23. blacklist.push(person);
  24. const personFound = peopleIn.find(p => p.firstName === person.firstName && p.lastName === person.lastName);
  25.  
  26. if (personFound) {
  27. const index = peopleIn.indexOf(personFound);
  28. peopleIn.splice(index, 1);
  29. peopleOut.push(person);
  30. }
  31. }
  32. });
  33.  
  34. let allPeopleObj = { peopleIn, peopleOut, blacklist };
  35.  
  36. if (sortCondition.criteria && sortCondition.action) {
  37. allPeopleObj[sortCondition.action]
  38. .sort((a, b) => a[sortCondition.criteria].localeCompare(b[sortCondition.criteria]))
  39. }
  40.  
  41. for (const action in allPeopleObj) {
  42. allPeopleObj[action] = allPeopleObj[action].map(person => JSON.stringify({firstName: person.firstName, lastName: person.lastName}));
  43. }
  44.  
  45. document.querySelector('#peopleIn p').textContent = allPeopleObj.peopleIn.join(' ');
  46. document.querySelector('#peopleOut p').textContent = allPeopleObj.peopleOut.join(' ');
  47. document.querySelector('#blacklist p').textContent = allPeopleObj.blacklist.join(' ');
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement