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);
如果这解决了您的问题,请告诉我。
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));
TA贡献1829条经验 获得超13个赞
你可以用它来过滤掉所有者信息,如果你想要更具体的东西,请评论下来x.filter(item => item.inventory.find(data => data.name.indexOf(searchKeyword) !== -1))
添加回答
举报