Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getData() {
- let textarea=JSON.parse(document.querySelector('textarea').value);
- let peopleInParagraph=document.querySelector('#peopleIn p');
- let peopleOutParagraph=document.querySelector('#peopleOut p');
- let blacklistParagraph=document.querySelector('#blacklist p');
- let lastElement=textarea.pop();
- let peopleIn=[];
- let peopleOut=[];
- let blacklist=[];
- for (const person of textarea) {
- let action=person.action;
- let currentPerson={
- firstName:person.firstName,
- lastName:person.lastName
- }
- if (action==='peopleIn') {
- if (!blacklist.find(p=>p.firstName===currentPerson.firstName &&
- p.lastName===currentPerson.lastName)) {
- peopleIn.push(currentPerson);
- }
- }else if (action==='peopleOut') {
- if (peopleIn.find(p=>p.firstName===currentPerson.firstName &&
- p.lastName===currentPerson.lastName)) {
- let index=peopleIn.findIndex(p=>p.firstName===currentPerson.firstName &&
- p.lastName===currentPerson.lastName);
- peopleIn.splice(index,1);
- peopleOut.push(currentPerson);
- }
- }else if (action==='blacklist') {
- if (peopleIn.find(p=>p.firstName===currentPerson.firstName &&
- p.lastName===currentPerson.lastName)) {
- let index=peopleIn.findIndex(p=>p.firstName===currentPerson.firstName &&
- p.lastName===currentPerson.lastName);
- peopleIn.splice(index,1);
- peopleOut.push(currentPerson);
- }
- blacklist.push(currentPerson);
- }
- }
- let output={};
- output['peopleIn']=peopleIn;
- output['peopleOut']=peopleOut;
- output['blacklist']=blacklist;
- if (lastElement.action!=='' &&
- lastElement.criteria!=='') {
- let criteria=lastElement.criteria;
- output[lastElement.action]=output[lastElement.action]
- .sort((a,b)=>a[criteria].localeCompare(b[criteria]));
- }
- peopleInParagraph.textContent=output.peopleIn
- .map(x=>JSON.stringify(x))
- .join(' ');
- peopleOutParagraph.textContent=output.peopleOut
- .map(x=>JSON.stringify(x))
- .join(' ');
- blacklistParagraph.textContent=output.blacklist
- .map(x=>JSON.stringify(x))
- .join(' ');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement