Last active
August 22, 2019 19:45
-
-
Save ErikGMatos/a12c4e2b14e0056f0b8b5cc39ece66a1 to your computer and use it in GitHub Desktop.
Função para se aplicar filtros com base em um objeto, no caso do React seria o State
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// não implementado | |
const data = [ | |
{ | |
"full_price": 2139.64, | |
"price_with_discount": 706.08, | |
"discount_percentage": 67.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Engenharia Mecânica", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "UNIP", | |
"score": 4.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unip.png" | |
}, | |
"campus": { | |
"name": "Jardim das Indústrias", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 1227.05, | |
"price_with_discount": 515.36, | |
"discount_percentage": 58.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Jornalismo", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "UNIP", | |
"score": 4.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unip.png" | |
}, | |
"campus": { | |
"name": "Água Branca", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1487.31, | |
"price_with_discount": 520.56, | |
"discount_percentage": 65.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Biomedicina", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "UNIP", | |
"score": 4.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unip.png" | |
}, | |
"campus": { | |
"name": "Jaguaré", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1668.4, | |
"price_with_discount": 617.31, | |
"discount_percentage": 63.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Arquitetura e Urbanismo", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Manhã" | |
}, | |
"university": { | |
"name": "UNIP", | |
"score": 4.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unip.png" | |
}, | |
"campus": { | |
"name": "Parque São Jorge", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1447.29, | |
"price_with_discount": 477.61, | |
"discount_percentage": 67.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Propaganda e Marketing", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "UNIP", | |
"score": 4.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unip.png" | |
}, | |
"campus": { | |
"name": "Paraíso", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1461.16, | |
"price_with_discount": 1241.99, | |
"discount_percentage": 15.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Jornalismo", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "Anhembi Morumbi", | |
"score": 4.2, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/anhembi-morumbi.png" | |
}, | |
"campus": { | |
"name": "Vila Olímpia", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1520.0, | |
"price_with_discount": 1368.0, | |
"discount_percentage": 10.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Marketing", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "Anhembi Morumbi", | |
"score": 4.2, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/anhembi-morumbi.png" | |
}, | |
"campus": { | |
"name": "Bela Vista", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1498.0, | |
"price_with_discount": 1273.3, | |
"discount_percentage": 15.0, | |
"start_date": "01/02/2020", | |
"enrollment_semester": "2020.1", | |
"enabled": false, | |
"course": { | |
"name": "Biomedicina", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Manhã" | |
}, | |
"university": { | |
"name": "Anhembi Morumbi", | |
"score": 4.2, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/anhembi-morumbi.png" | |
}, | |
"campus": { | |
"name": "Vila Olímpia", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1745.22, | |
"price_with_discount": 539.1, | |
"discount_percentage": 69.11, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Engenharia Mecânica", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Manhã" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "Paulista", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 1367.34, | |
"price_with_discount": 539.14, | |
"discount_percentage": 60.57, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Ciência da Computação", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "Paulista", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 2038.19, | |
"price_with_discount": 1259.02, | |
"discount_percentage": 38.24, | |
"start_date": "01/02/2020", | |
"enrollment_semester": "2020.1", | |
"enabled": false, | |
"course": { | |
"name": "Gastronomia", | |
"kind": "Presencial", | |
"level": "Tecnólogo", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "Anália Franco", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 437.0, | |
"price_with_discount": 142.9, | |
"discount_percentage": 67.3, | |
"start_date": "01/02/2020", | |
"enrollment_semester": "2020.1", | |
"enabled": true, | |
"course": { | |
"name": "Jogos Digitais", | |
"kind": "EaD", | |
"level": "Tecnólogo", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "Liberdade", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 453.0, | |
"price_with_discount": 186.82, | |
"discount_percentage": 58.76, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Ciência da Computação", | |
"kind": "EaD", | |
"level": "Bacharelado", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "São Miguel Paulista", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 453.0, | |
"price_with_discount": 163.03, | |
"discount_percentage": 64.01, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Sistemas de Informação", | |
"kind": "EaD", | |
"level": "Bacharelado", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "Liberdade", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 499.0, | |
"price_with_discount": 159.23, | |
"discount_percentage": 68.09, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Ciências Econômicas", | |
"kind": "EaD", | |
"level": "Bacharelado", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "UNICSUL", | |
"score": 3.9, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unicsul.png" | |
}, | |
"campus": { | |
"name": "Paulista", | |
"city": "São Paulo" | |
} | |
}, | |
{ | |
"full_price": 998.33, | |
"price_with_discount": 748.75, | |
"discount_percentage": 25.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Gestão de Recursos Humanos", | |
"kind": "Presencial", | |
"level": "Tecnólogo", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "Anhanguera", | |
"score": 3.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/anhanguera.png" | |
}, | |
"campus": { | |
"name": "Rio Comprido", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 1408.63, | |
"price_with_discount": 493.02, | |
"discount_percentage": 65.0, | |
"start_date": "01/02/2020", | |
"enrollment_semester": "2020.1", | |
"enabled": true, | |
"course": { | |
"name": "Arquitetura e Urbanismo", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "Anhanguera", | |
"score": 3.5, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/anhanguera.png" | |
}, | |
"campus": { | |
"name": "Rio Comprido", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 1385.57, | |
"price_with_discount": 692.79, | |
"discount_percentage": 50.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Farmácia", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Manhã" | |
}, | |
"university": { | |
"name": "Estácio", | |
"score": 4.1, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/estacio.png" | |
}, | |
"campus": { | |
"name": "Patriolino Ribeiro", | |
"city": "Fortaleza" | |
} | |
}, | |
{ | |
"full_price": 414.14, | |
"price_with_discount": 207.07, | |
"discount_percentage": 50.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Sistemas de Informação", | |
"kind": "EaD", | |
"level": "Bacharelado", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "Estácio", | |
"score": 4.1, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/estacio.png" | |
}, | |
"campus": { | |
"name": "Centro", | |
"city": "Jacareí" | |
} | |
}, | |
{ | |
"full_price": 414.14, | |
"price_with_discount": 207.07, | |
"discount_percentage": 50.0, | |
"start_date": "01/02/2020", | |
"enrollment_semester": "2020.1", | |
"enabled": true, | |
"course": { | |
"name": "Gestão de Recursos Humanos", | |
"kind": "EaD", | |
"level": "Tecnólogo", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "Estácio", | |
"score": 4.1, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/estacio.png" | |
}, | |
"campus": { | |
"name": "Centro", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 428.43, | |
"price_with_discount": 278.48, | |
"discount_percentage": 35.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Administração", | |
"kind": "EaD", | |
"level": "Bacharelado", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "Estácio", | |
"score": 4.1, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/estacio.png" | |
}, | |
"campus": { | |
"name": "Centro", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 519.71, | |
"price_with_discount": 222.23, | |
"discount_percentage": 57.24, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "História", | |
"kind": "EaD", | |
"level": "Licenciatura", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "Unopar", | |
"score": 4.0, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unopar.png" | |
}, | |
"campus": { | |
"name": "São José dos Campos", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 454.56, | |
"price_with_discount": 174.01, | |
"discount_percentage": 61.72, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Gestão de Recursos Humanos", | |
"kind": "EaD", | |
"level": "Tecnólogo", | |
"shift": "Virtual" | |
}, | |
"university": { | |
"name": "Unopar", | |
"score": 4.0, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/unopar.png" | |
}, | |
"campus": { | |
"name": "Motorama", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 1219.86, | |
"price_with_discount": 609.93, | |
"discount_percentage": 50.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Administração", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "ETEP", | |
"score": 3.2, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/etep.png" | |
}, | |
"campus": { | |
"name": "Jardim Esplanada", | |
"city": "São José dos Campos" | |
} | |
}, | |
{ | |
"full_price": 932.58, | |
"price_with_discount": 606.18, | |
"discount_percentage": 35.0, | |
"start_date": "01/08/2019", | |
"enrollment_semester": "2019.2", | |
"enabled": true, | |
"course": { | |
"name": "Educação Física", | |
"kind": "Presencial", | |
"level": "Bacharelado", | |
"shift": "Noite" | |
}, | |
"university": { | |
"name": "ETEP", | |
"score": 3.2, | |
"logo_url": "https://www.tryimg.com/u/2019/04/16/etep.png" | |
}, | |
"campus": { | |
"name": "Jardim Esplanada", | |
"city": "São José dos Campos" | |
} | |
} | |
] | |
const newList = { | |
cidade: 'São José dos Campos', | |
curso: '', | |
presencial: 'Presencial', | |
ead: 'EaD', | |
preço: 500, | |
}; | |
// item.xxxx será o local de onde será verificado cada propriedade ex: item.course.name === curso | |
function mountQuery(newList){ | |
let sql2 = ''; | |
if (newList.cidade) { | |
sql2 += `item.campus.city === '${newList.cidade}' && `; | |
} | |
if (newList.curso) { | |
sql2 += `item.course.name === '${newList.curso}' && `; | |
} | |
if (newList.presencial && newList.ead) { | |
sql2 += `(item.course.kind === '${newList.presencial}' || item.course.kind === '${newList.ead}') && `; | |
} else if (newList.presencial) { | |
sql2 += `item.course.kind === '${newList.presencial}' && `; | |
} else if (newList.ead) { | |
sql2 += `item.course.kind === '${newList.ead}' && `; | |
} | |
if (newList.preço) { | |
sql2 += `item.price_with_discount <= ${newList.preço} && `; | |
} | |
// sql2; | |
sql2 = sql2.substring(0, sql2.length - 4); // Remove os dois && e os dois espaços do final da string | |
return sql2; | |
} | |
var query = mountQuery(newList); | |
const filtered = data.filter(item => eval(query)); | |
console.log(filtered); | |
console.log(query); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Função para se aplicar filtros