Skip to content

Instantly share code, notes, and snippets.

@arnarthor
Created November 1, 2016 22:02
Show Gist options
  • Save arnarthor/193fddc24358e401fd5be35d2ba083e6 to your computer and use it in GitHub Desktop.
Save arnarthor/193fddc24358e401fd5be35d2ba083e6 to your computer and use it in GitHub Desktop.
const express = require('express');
const cars = [{type: 'Nissan', model: 2016}, {type: 'Nissan', model: 2015}, {type: 'Toyota', model: 2016}, {type: 'Kia', model: 2016}]
const punches = [{count: 5, name: 'Arnar'}, {count: 6, name: 'Daníel'}, {count: 10, name: 'Axel'}];
const app = express();
app.get('/goodCarFilter/:year', (req, res) => {
const filteredCars = cars.filter((car) => {
return car.model === parseInt(req.params.year);
})
res.json(filteredCars);
});
app.get('/badCarFilter/:year', (req, res) => {
const filteredCars = [];
cars.forEach((car) => {
if (car.model === parseInt(req.params.year)) {
filteredCars.push(car);
}
});
res.json(filteredCars);
});
app.get('/goodPunchedInUsers', (req, res) => {
const users = punches.map((punch) => {
return punch.name;
});
res.json(users);
});
app.get('/badPunchedInUsers', (req, res) => {
let users = [];
for(var i = 0; i < punches.length; i++) {
users.push(punches[i].name);
}
res.json(users);
});
app.get('/goodPunchMap', (req, res) => {
const punchMap = punches.reduce((accumulated, currentElement) => {
return Object.assign(accumulated, {[currentElement.name]: currentElement});
}, {});
res.json(punchMap);
});
app.get('/badPunchMap', (req, res) => {
const punchMap = {};
punches.forEach((punch) => {
punchMap[punch.name] = punch;
});
res.json(punchMap);
});
app.listen(5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment