map 取值 - daniel-qa/Vue GitHub Wiki
map 取值
在 JavaScript 的 map 遍歷中,只有 return 的物件才會被組成新的陣列。
map 內部用 push 只是暫時把資料收集到一個變數(如 tmIds),
最後 return 的物件才會成為 noticeHistory.value 陣列的每一筆資料。
noticeHistory.value = response.map((item) => {
const searchContent = [];
// 取得所有 tmid(不考慮 mode)
let tmIds = [];
if (Array.isArray(item.search)) {
item.search.forEach((s) => {
// 只要有 tmId 就收集
if (s.tmId) {
if (Array.isArray(s.tmId)) {
tmIds.push(...s.tmId); // 展開陣列,逐一加入
}
}
// 你原本的 searchContent 處理
// ...(略,保留原本 searchContent 處理邏輯)
});
}
return {
id: item.id,
// ...其他欄位
searchContent,
tmIds, // 這裡直接放所有抓到的 tmId 陣列
// ...其他欄位
};
}).sort((a, b) => b.sendTimestamp - a.sendTimestamp);