第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將特定的 props 映射到 state?

如何將特定的 props 映射到 state?

瀟瀟雨雨 2023-11-02 22:19:10
我正在嘗試將特定的道具映射到狀態(tài)brands。另外,不要添加狀態(tài)中已存在的值brands。目標(biāo)輸出:brands: [  "IKEA",  "Tesla"]我的嘗試const mapState = (state) => ({  brands: state.products.forEach(product => {    if (product.brands) {      return product.brands;    }  })});export default connect(mapState)(Brands);狀態(tài):products: [  {    "id": 1,    "title": "Pen",    "brands": "IKEA"  },  {    "id": 2,    "title": "Flamethrower",    "brands": "Tesla"  },  {    "id": 3,    "title": "T-shirt",    "brands": "Tesla"  }]
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

.reduce您可以通過使用[1] 構(gòu)建一系列不同的產(chǎn)品來執(zhí)行以下操作。


const mapState = (state) => ({

  brands: state

     .products

     .reduce((brands, product) => {

        if (!brands.includes(product.brands)) {

           return brands.concat([product.brands]);

        }

        return brands;

     }, [])

  })

});


export default connect(mapState)(Brands);

如果您需要在應(yīng)用程序中重用此函數(shù),則可以將檢索不同品牌的功能提取到函數(shù)中:


function getProductBrands(products) {

   return products

     .reduce((brands, product) => {

        if (!brands.includes(product.brands)) {

           return brands.concat([product.brands]);

        }

        return brands;

     }, []);

}

參考文獻(xiàn):[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce


查看完整回答
反對(duì) 回復(fù) 2023-11-02
?
翻翻過去那場雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

forEach不返回任何內(nèi)容。您可以做的是創(chuàng)建一個(gè)函數(shù)getAllProductBrands并調(diào)用該函數(shù)。然后,此函數(shù)應(yīng)返回所有品牌的列表。


const getAllProductBrands = (products) => {

  const brands = [];

  products.forEach(product => {

    if(!brands.some(brand => brand === product.brand) {

      brands = [...brands, product.brand];    

    };

  });

};

然后你可以在你的mapState中調(diào)用這個(gè)函數(shù)。


const mapState = (state) => ({

  brands: getAllProductBrands(state.products)

});


查看完整回答
反對(duì) 回復(fù) 2023-11-02
?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

獲取唯一的品牌列表


const mapState = ({products}) => {

  const brands = [...new Set( products

     .map(product => product.brands))];

  return { brands }

};


查看完整回答
反對(duì) 回復(fù) 2023-11-02
  • 3 回答
  • 0 關(guān)注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)