为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

倚天杖 2022-08-18 10:09:44
我正在尝试在我的 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",  },];如何过滤这些?
查看完整描述

3 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

不确定要如何过滤它也会共享所需的输出以获得更好的清晰度。


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);


如果这解决了您的问题,请告诉我。


查看完整回答
反对 回复 2022-08-18
?
手掌心

TA贡献1942条经验 获得超3个赞

您可以映射和过滤数据。


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 objects

console.log(findInventory(data));

// get names

console.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 objects

console.log(findInventory(data));

// get names by filter

console.log(findInventory(data, (i) => i.name, (name) => name.indexOf("AK-") !== -1));


查看完整回答
反对 回复 2022-08-18
?
烙印99

TA贡献1829条经验 获得超13个赞

你可以用它来过滤掉所有者信息,如果你想要更具体的东西,请评论下来x.filter(item => item.inventory.find(data => data.name.indexOf(searchKeyword) !== -1))



查看完整回答
反对 回复 2022-08-18
  • 3 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信