[英]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.