[英]How to map and filter array of values based on value from another array in javascript?
这里我有两个数组。
一个数组将保存 ID,另一个数组保存数据。 我想通过使用 Id 数组中的值进行过滤来映射数据中的值。
例如:如果 Ids 数组中的 6 与 data.id 匹配,则意味着我需要获取 assets 数组中的值。 我已经完成了那个解决方案。 但是我的解决方案似乎很难理解。
const ids = [
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 5, 4, 4, 4
];
const data = [
{
id: 10,
assets: [
{
type: 'First Section',
value: 60
},
{
type: 'Second Section',
value: 40
}
]
},
{
id: 11,
assets: [
{
type: 'First Section',
value: 65
},
{
type: 'Second Section',
value: 35
}
]
},
{
id: 12,
assets: [
{
type: 'First Section',
value: 70
},
{
type: 'Second Section',
value: 30
}
]
},
{
id: 13,
assets: [
{
type: 'First Section',
value: 75
},
{
type: 'Second Section',
value: 25
}
]
},
{
id: 14,
assets: [
{
type: 'First Section',
value: 80
},
{
type: 'Second Section',
value: 20
}
]
},
{
id: 15,
assets: [
{
type: 'First Section',
value: 85
},
{
type: 'Second Section',
value: 15
}
]
},
{
id: 16,
assets: [
{
type: 'First Section',
value: 90
},
{
type: 'Second Section',
value: 10
}
]
},
{
id: 2,
assets: [
{
type: 'First Section',
value: 20
},
{
type: 'Second Section',
value: 80
}
]
},
{
id: 3,
assets: [
{
type: 'First Section',
value: 25
},
{
type: 'Second Section',
value: 75
}
]
},
{
id: 4,
assets: [
{
type: 'First Section',
value: 30
},
{
type: 'Second Section',
value: 70
}
]
},
{
id: 5,
assets: [
{
type: 'First Section',
value: 35
},
{
type: 'Second Section',
value: 65
}
]
},
{
id: 6,
assets: [
{
type: 'First Section',
value: 40
},
{
type: 'Second Section',
value: 60
}
]
},
{
id: 7,
assets: [
{
type: 'First Section',
value: 45
},
{
type: 'Second Section',
value: 55
}
]
},
{
id: 8,
assets: [
{
type: 'First Section',
value: 50
},
{
type: 'Second Section',
value: 50
}
]
},
{
id: 9,
assets: [
{
type: 'First Section',
value: 55
},
{
type: 'Second Section',
value: 45
}
]
},
{
id: 1,
assets: [
{
type: 'First Section',
value: 15
},
{
type: 'Second Section',
value: 85
}
]
}
];
在这里,我只需要获取 assets 数组中的第一个部分值。
我已经通过使用下面的代码做到了这一点
let res = ids.map((val) => data.filter(v => v.id === val)[0]).map(v => v.assets.filter(v => v.type === 'First Section')[0]).map(v => v.value)
console.log(res)
输出
[
40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
40, 40, 40, 40, 35, 30, 30, 30
]
但是有什么方法可以进一步优化功能。 请指导我
您正确地感觉到您的算法虽然有效,但效率极低。 您必须在整个数据数组中搜索 ids 中的每个项目,这使其成为 O(n^m) 时间复杂度,根本无法很好地扩展。
我认为最好和最有效的方法是为数据列表中的每个项目创建一个 id -> first section 值的映射。 从那里,获取任何 ID 的部分值是一个非常简单且快速的查找。 这是可以做到的。 它只需要 1 次通过数据数组,1 次通过 ids 数组 - O(n + m),如下所示:
const ids = [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, 4, 4 ]; const data = [{"id":10,"assets":[{"type":"First Section","value":60},{"type":"Second Section","value":40}]},{"id":11,"assets":[{"type":"First Section","value":65},{"type":"Second Section","value":35}]},{"id":12,"assets":[{"type":"First Section","value":70},{"type":"Second Section","value":30}]},{"id":13,"assets":[{"type":"First Section","value":75},{"type":"Second Section","value":25}]},{"id":14,"assets":[{"type":"First Section","value":80},{"type":"Second Section","value":20}]},{"id":15,"assets":[{"type":"First Section","value":85},{"type":"Second Section","value":15}]},{"id":16,"assets":[{"type":"First Section","value":90},{"type":"Second Section","value":10}]},{"id":2,"assets":[{"type":"First Section","value":20},{"type":"Second Section","value":80}]},{"id":3,"assets":[{"type":"First Section","value":25},{"type":"Second Section","value":75}]},{"id":4,"assets":[{"type":"First Section","value":30},{"type":"Second Section","value":70}]},{"id":5,"assets":[{"type":"First Section","value":35},{"type":"Second Section","value":65}]},{"id":6,"assets":[{"type":"First Section","value":40},{"type":"Second Section","value":60}]},{"id":7,"assets":[{"type":"First Section","value":45},{"type":"Second Section","value":55}]},{"id":8,"assets":[{"type":"First Section","value":50},{"type":"Second Section","value":50}]}]; // for each item in data, map id to first section value for easy lookup let sectionMap = data.reduce((res, curr) => { res[curr.id] = curr.assets.find(asset => asset.type === 'First Section').value; return res; }, {}); console.log(sectionMap); // map the id to its respective first section value with the lookup map we created let result = ids.map(id => sectionMap[id]); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.