[英]How can I loop through the keys of an object and set values equal to a destructured variable?
我正在尝试简化和缩短我当前的代码。 我的代码工作正常。 这就是我所拥有的:
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');
})
如何使重复的后半部分代码更短? 可能有一种方法我只能写一行
您可以使用扩展运算符。
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');
})
这并不短,但部分原因是我们将所有内容集中在一个地方。 如果你有一个工具 function 来复制这些属性,你可以在需要时调用它。
部分应用功能完全取决于个人喜好♂️
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);
您可以将Object.assign()
与扩展运算符结合使用,将附加属性添加到找到的列表中,如下所示:
const foundListing = listings.find(l => l.id === id);
Object.assign(foundListing, ...req.body)
甚至更短(因为您以后可能不需要 foundListing 变量):
Object.assign(listings.find(l => l.id === id), ...req.body);
突然你的代码变得很短:
app.patch('/listings/:id', (req, res) => {
Object.assign(listings.find(l => l.id === req.params.id), ...req.body);
res.redirect('/listings');
})
如果要修改所有foundListing
属性,那么您可以遍历属性进行更改,如下所示:
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');
})
如果您需要指定要修改的属性,则可以将它们列在数组中并循环通过它们进行更改,如下所示:
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');
})
我会使用以下方法。
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');
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.