亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何過濾數組中哪些元素具有嵌套數組?

如何過濾數組中哪些元素具有嵌套數組?

梵蒂岡之花 2023-11-11 21:22:33
我試圖找到能夠過濾屬性中包含另一個數組的數組的邏輯。見下文: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,    },];正如您在上面看到的,我想過濾數組并僅顯示那些在類型中包含過濾器 val 的產品。我已經嘗試過,但我的解決方案真的很長。
查看完整描述

2 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

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

.filter您可以在外部數組和.includes內部數組上使用來執行您要查找的操作。

根據記錄,“berry”從未出現在任何“type”數組中,但“berry”卻出現在


查看完整回答
反對 回復 2023-11-11
?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

您可以使用Array.prototype.filter()withArray.prototype.some()來獲取過濾結果。如果數組中至少有一個元素通過給定回調函數的測試,則 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);


查看完整回答
反對 回復 2023-11-11
  • 2 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號