I am trying to simplify and shorten my current code. My code is working fine. This is what I have:
app.patch('/listings/:id', (req, res) => {
const { id } = req.params;
console.log(req.body)
const {
leaser, propertyType, pricePerDay,
currency, city, street, description
} = req.body;
const foundListing = listings.find(l => l.id === id);
foundListing.leaser = leaser;
foundListing.pricePerDay = pricePerDay;
foundListing.propertyType = propertyType;
foundListing.currency = currency;
foundListing.city = city;
foundListing.street = street;
foundListing.description = description;
res.redirect('/listings');
})
How can I make the repetitive last half of the code shorter? There is probably a way in which I can write only one line
You can use the spread operator .
app.patch('/listings/:id', (req, res) => {
const { id } = req.params;
console.log(req.body)
const {
leaser, propertyType, pricePerDay,
currency, city, street, description
} = req.body;
const foundListing = {
...listings.find(l => l.id === id),
...req.body
};
res.redirect('/listings');
})
This isn't much shorter, but that's partly because we have it all in one place. If you had a util function to copy these properties, you cna just call it when needed.
Partially applying the functions is entirely down to personal preference ♂️
const listings = [ { id: 1, name: "a" }, { id: 2, name: "b" }, ]; const copyProperties = ps => from => to => { ps.forEach(p => { to[p] = from[p]; }); } const applyRequestToListing = req => copyProperties([ "leaser", "pricePerDay", "propertyType", "currency", "city", "street", "description", ])(req); const handleRequest = (req) => { const foundListing = listings.find(l => { return l.id === req.params.id }); applyRequestToListing(req.body)(foundListing); }; handleRequest({ params: { id: 2, }, body: { leaser: "leaserIn", pricePerDay: "pricePerDayIn", propertyType: "propertyTypeIn", currency: "currencyIn", city: "cityIn", street: "streetIn", description: "descriptionIn", }, }); console.log(listings);
You can use Object.assign()
in combination with the spread operator to add the additional properties to the found listing like this:
const foundListing = listings.find(l => l.id === id);
Object.assign(foundListing, ...req.body)
Or even shorter (as you probably don't need the foundListing variable later):
Object.assign(listings.find(l => l.id === id), ...req.body);
Suddenly your code becomes really short:
app.patch('/listings/:id', (req, res) => {
Object.assign(listings.find(l => l.id === req.params.id), ...req.body);
res.redirect('/listings');
})
If all the foundListing
properties are going to be modified, then you could loop through the properties to make changes, like so:
app.patch('/listings/:id', (req, res) => {
const { id } = req.params,
foundListing = listings.find(l => l.id === id);
for(const key in foundListing){
foundListing[key] = req.body[key]
}
res.redirect('/listings');
})
If you need to specify which properties are going to be modified then you can list them in an array and loop through them to make changes, like so:
app.patch('/listings/:id', (req, res) => {
const { id } = req.params,
foundListing = listings.find(l => l.id === id),
keys = [
'leaser',
'propertyType',
'pricePerDay',
'currency',
'city',
'street',
'description'
];
keys.forEach(key => { foundListing[key] = req.body[key] })
res.redirect('/listings');
})
I would use the following approach.
app.patch('/listings/:id', (req, res) => {
const { id } = req.params;
const foundListing = listings.find(l => l.id === id);
Object.keys(foundListing).forEach(key => {
foundListing[key] = req.body[key]
})
res.redirect('/listings');
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.