簡體   English   中英

如何遍歷 object 的鍵並設置等於解構變量的值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM