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

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

如何在兩個深度數組中進行過濾

如何在兩個深度數組中進行過濾

一只萌萌小番薯 2023-12-14 14:37:19
我想要過濾兩個深層數組,實際上是我的 JSON:{  "0": {    "product":[{      "uuid":"uid",      "name":"Rice"    },    {      "uuid":"uid",      "name":"Pasta"    }]  },  "1": {    "product":[{      "uuid":"uid",      "name":"Milk"    }]  }}當我用“ric”一詞進行過濾時,我希望得到類似的結果:{  "0": {    "product":[{      "uuid":"uid",      "name":"Rice"    }]  }}但我得到了這個結果:{  "0": {    "product":[{      "uuid":"uid",      "name":"Rice"    },    {      "uuid":"uid",      "name":"Pasta"    }]  }}我的代碼:dataSort.categories = json 和 event.target.value.toLowerCase() = 特定單詞dataSort.categories.filter(s => s.products.find(p => p.name.toLowerCase().includes(event.target.value.toLowerCase())));
查看完整描述

4 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

您可以通過組合reduce和來實現這一點filter


var input = {

  "0": {

    "product":[{

      "uuid":"uid",

      "name":"Rice"

    },

    {

      "uuid":"uid",

      "name":"Pasta"

    }]

  },

  "1": {

    "product":[{

      "uuid":"uid",

      "name":"Milk"

    }]

  }

}


var search = "ric"


var result = Object.entries(input).reduce( (acc, [key,val]) => {

  found = val.product.filter(x => x.name.toLowerCase().includes(search.toLowerCase()))

  if(found.length){

    acc[key] = {...val, product: found}

  }

  return acc

},{})


console.log(result)


查看完整回答
反對 回復 2023-12-14
?
滄海一幻覺

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

有很多方法可以做到這一點,一種是將頂級數組映射到子數組過濾結果,然后對其進行過濾:

dataSort.categories
  .map(s => s.products.filter(p => p.name.toLowerCase().includes(event.target.value.toLowerCase())))
  .filter(s => !!s.products.length);

您可能還更喜歡獲得“平面”數組作為結果,因為在之后使用它更容易:

dataSort.categories
  .reduce((acc, s) => [...acc, s.products.filter(p => p.name.toLowerCase().includes(event.target.value.toLowerCase()))], []);



查看完整回答
反對 回復 2023-12-14
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

您的數據集是一個Object,而不是一個Array,并且過濾器是一個 Array 方法。您可以通過Object.values循環對象值來使用reduce,然后過濾您的產品數組。

const data = {

? '0': {

? ? product: [

? ? ? {

? ? ? ? uuid: 'uid',

? ? ? ? name: 'Rice',

? ? ? },

? ? ? {

? ? ? ? uuid: 'uid',

? ? ? ? name: 'Pasta',

? ? ? },

? ? ],

? },

? '1': {

? ? product: [

? ? ? {

? ? ? ? uuid: 'uid',

? ? ? ? name: 'Milk',

? ? ? },

? ? ],

? },

};


const keyword = 'ric';

const dataset = Object.values(data);

const results = dataset.reduce((acc, item, index) => {

? const search = keyword.toLowerCase();


? const product = item.product.filter(product => product.name.toLowerCase().includes(search));

? if (product.length) acc[index] = { ...item, product };


? return acc;

}, {});


console.log(results);



查看完整回答
反對 回復 2023-12-14
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

請在下面找到代碼來過濾掉里面的值product.name,并且只返回與數組中的相等條件匹配的值product。


const json = [

  {

    product: [

      {

        uuid: "uid",

        name: "Rice",

      },

      {

        uuid: "uid",

        name: "Pasta",

      },

    ],

  },

  {

    product: [

      {

        uuid: "uid",

        name: "Milk",

      },

    ],

  },

];

const inputValue = "rIc";

const filteredArray = [];


json.map((s) => {

  const item = s.product.find((p) =>

    p.name.toLowerCase().includes(inputValue.toLowerCase())

  );

  item && filteredArray.push({ product: item });

});


console.dir(filteredArray);


查看完整回答
反對 回復 2023-12-14
  • 4 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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