使搜索函数筛选深层嵌套对象项

我正在尝试在我的 React 应用程序中创建一个搜索函数,通过在数组中检查其中的项目来过滤掉一个项目。我有使用和绘制搜索中特定项目的想法。但是,从后端提取的对象具有用户将搜索的非常深入嵌套的项目。.filter().includes()


下面是一个对象的示例,其中包含我尝试筛选注释的项目:


const x = [

  {

    owner: "76561198111463361",

    sold: true,

    inventory: [

      {

        name: "AK-47 | Redline", //User may look for this

        price: 1369,

      },

      {

        name: "StatTrak™ AWP | Asiimov", //User may look for this

        price: 8050,

      },

    ],

    name: "m",

    password: "e",

  },

  {

    owner: "76561198111463361",

    sold: true,

    inventory: [

      {

        name: "AK-47 | Redline", //User may look for this

        price: 1369,

      },

      {

        name: "StatTrak™ AWP | Asiimov", //User may look for this

        price: 8050,

      },

    ],

    name: "m",

    password: "e",

  },

];

如何过滤这些?


倚天杖
浏览 111回答 3
3回答

月关宝盒

不确定要如何过滤它也会共享所需的输出以获得更好的清晰度。var data=[{owner: "76561198111463361",sold: true,inventory: [{ name: "AK-47 | Redline", }, { name: "StatTrak™ AWP | Asiimov",}],name: "m",password: "e",},{owner: "76561198111463361",sold: true,inventory: [{ name: "AK-47 | Redline", }, { name: "StatTrak™ AWP | Asiimov",}],name: "m",password: "e"}];var string = 'Redline';result = data.filter(val=>val.inventory.some(k=>k.name.toUpperCase().includes(string.toUpperCase())));console.log(result);如果这解决了您的问题,请告诉我。

手掌心

您可以映射和过滤数据。const findInventory = (data, fn) => {  return data.reduce((array, item) => {    let inventory = item.inventory;    if (fn !== undefined) {      inventory = inventory.map(fn);    }    array = array.concat(inventory);    return array;  }, []);};const data = [{"owner":"76561198111463361","sold":true,"inventory":[{"name":"AK-47 | Redline","price":1369},{"name":"StatTrak™ AWP | Asiimov","price":8050}],"name":"m","password":"e"},{"owner":"76561198111463361","sold":true,"inventory":[{"name":"AK-47 | Redline","price":1369},{"name":"StatTrak™ AWP | Asiimov","price":8050}],"name":"m","password":"e"}];// Get objectsconsole.log(findInventory(data));// get namesconsole.log(findInventory(data, (i) => i.name));第二个例子:const findInventory = (data, mapper, filter) => {  return data.reduce((array, item) => {    let inventory = item.inventory;    if (mapper !== undefined && filter !== undefined) {      inventory = inventory.map(mapper).filter(filter);    }    array = array.concat(inventory);    return array;  }, []);};const data = [{"owner":"76561198111463361","sold":true,"inventory":[{"name":"AK-47 | Redline","price":1369},{"name":"StatTrak™ AWP | Asiimov","price":8050}],"name":"m","password":"e"},{"owner":"76561198111463361","sold":true,"inventory":[{"name":"AK-47 | Redline","price":1369},{"name":"StatTrak™ AWP | Asiimov","price":8050}],"name":"m","password":"e"}];// Get objectsconsole.log(findInventory(data));// get names by filterconsole.log(findInventory(data, (i) => i.name, (name) => name.indexOf("AK-") !== -1));

烙印99

你可以用它来过滤掉所有者信息,如果你想要更具体的东西,请评论下来x.filter(item => item.inventory.find(data => data.name.indexOf(searchKeyword) !== -1))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript