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

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

如何過(guò)濾數(shù)組中哪些元素具有嵌套數(shù)組?

如何過(guò)濾數(shù)組中哪些元素具有嵌套數(shù)組?

梵蒂岡之花 2023-11-11 21:22:33
我試圖找到能夠過(guò)濾屬性中包含另一個(gè)數(shù)組的數(shù)組的邏輯。見(jiàn)下文:let filterValue = 'berries'; const products = [    {        id: 1,        productName: 'Strawberry Basil',        productImgURL:            'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/Cherry_Pop_Still_4K_Front-CherryPop.png?v=1588713373',        type: ['berry', 'citrusy', 'fancy'],        price: 5.5,    },    {        id: 2,        productName: 'Sour Blueberry',        productImgURL:            'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/SourBlueBerry_Still_4K_Front-SourBlueberry.png?v=1588713584',        type: ['all', 'sour', 'berry'],        price: 4,    },    {        id: 3,        productName: 'Blackberry Jam',        productImgURL:            'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/BlackBerry_Jam_Still_4K_Front-BlackberryJam.png?v=1595035965',        type: ['all', 'berry'],        price: 10,    },    {        id: 4,        productName: 'Orange Nectarine',        productImgURL:            'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/Orange_Nectarine_Still_4K_Front-OrangeNectarine.png?v=1588713522',        type: ['all', 'Citrus', 'fancy', 'juicy'],        price: 6,    },    {        id: 5,        productName: 'Lemon Verbena',        productImgURL:            'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/Lemon_Verbena_Still_4K_Front-LemonVerbena.png?v=1588713474',        type: ['all', 'Citrus', 'classic', 'floral'],        price: 4.5,    },    {        id: 6,        productName: 'Extra Peach',        productImgURL:            'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/ExtraPeach_Still_4K_Front-ExtraPeach.png?v=1588713411',        type: ['Juicy'],        price: 8.5,    },];正如您在上面看到的,我想過(guò)濾數(shù)組并僅顯示那些在類型中包含過(guò)濾器 val 的產(chǎn)品。我已經(jīng)嘗試過(guò),但我的解決方案真的很長(zhǎng)。
查看完整描述

2 回答

?
天涯盡頭無(wú)女友

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

const filteredProducts = products.filter(p => p.type.includes(type));

.filter您可以在外部數(shù)組和.includes內(nèi)部數(shù)組上使用來(lái)執(zhí)行您要查找的操作。

根據(jù)記錄,“berry”從未出現(xiàn)在任何“type”數(shù)組中,但“berry”卻出現(xiàn)在


查看完整回答
反對(duì) 回復(fù) 2023-11-11
?
慕田峪4524236

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

您可以使用Array.prototype.filter()withArray.prototype.some()來(lái)獲取過(guò)濾結(jié)果。如果數(shù)組中至少有一個(gè)元素通過(guò)給定回調(diào)函數(shù)的測(cè)試,則 some() 方法返回 true。


const filterValue = 'berry';


const products = [

  {

    id: 1,

    productName: 'Strawberry Basil',

    productImgURL:

      'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/Cherry_Pop_Still_4K_Front-CherryPop.png?v=1588713373',

    type: ['berry', 'citrusy', 'fancy'],

    price: 5.5,

  },

  {

    id: 2,

    productName: 'Sour Blueberry',

    productImgURL:

      'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/SourBlueBerry_Still_4K_Front-SourBlueberry.png?v=1588713584',

    type: ['all', 'sour', 'berry'],

    price: 4,

  },

  {

    id: 3,

    productName: 'Blackberry Jam',

    productImgURL:

      'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/BlackBerry_Jam_Still_4K_Front-BlackberryJam.png?v=1595035965',

    type: ['all', 'berry'],

    price: 10,

  },

  {

    id: 4,

    productName: 'Orange Nectarine',

    productImgURL:

      'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/Orange_Nectarine_Still_4K_Front-OrangeNectarine.png?v=1588713522',

    type: ['all', 'Citrus', 'fancy', 'juicy'],

    price: 6,

  },

  {

    id: 5,

    productName: 'Lemon Verbena',

    productImgURL:

      'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/Lemon_Verbena_Still_4K_Front-LemonVerbena.png?v=1588713474',

    type: ['all', 'Citrus', 'classic', 'floral'],

    price: 4.5,

  },

  {

    id: 6,

    productName: 'Extra Peach',

    productImgURL:

      'https://cdn.shopify.com/s/files/1/0274/3641/7123/products/ExtraPeach_Still_4K_Front-ExtraPeach.png?v=1588713411',

    type: ['Juicy'],

    price: 8.5,

  },

];


const ret = products.filter(({ type }) =>

  type.some((x) => x.toLowerCase() === filterValue.toLowerCase())

);

console.log(ret);


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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