js array元素重復,怎麼刪除, 只留一個
JavaScript 中刪除陣列重複元素,只留一個的最簡單且最高效的方法是使用 ES6 的 Set。以下為您介紹幾種常見且實用的處理方式: [1]
1. 推薦:使用 Set (最簡單、最快)
Set 是 ES6 新增的資料結構,它只允許儲存不重複的值。搭配展開運算子 ...,可以一行程式碼搞定。 [2]
const arr = [1, 2, 2, 3, 4, 4, 1];
const uniqueArr = [...new Set(arr)];
// 結果: [1, 2, 3, 4]
2. 使用 filter 與 indexOf (相容性最佳)
這個方法會過濾陣列,檢查元素首次出現的索引是否等於當前索引,如果不是(代表重複了),就會被過濾掉。 [3]
const arr = [1, 2, 2, 3, 4, 4, 1];
const uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index);
// 結果: [1, 2, 3, 4]
3. 進階:陣列內為「物件」的去重
如果你的陣列元素是物件,無法直接用 Set 去重。此時可以利用 Map 或 filter 配合唯一識別鍵(例如 id)來保留單一物件。 [4, 5]
const users = [{id: 1, name: 'A'}, {id: 2, name: 'B'}, {id: 1, name: 'A'}];
// 使用 Map 根據 id 去重
const uniqueUsers = [...new Map(users.map(item => [item.id, item])).values()];
// 結果: [{id: 1, name: 'A'}, {id: 2, name: 'B'}]
如需了解更多關於陣列操作的底層細節,可參考 MDN Web Docs 的完整指南。
[1] https://gotraveltoworld.medium.com
[3] https://www.explainthis.io
[4] https://eruditeness.news.blog
Q2: 元素是url,路徑一樣,就刪
在 JavaScript 中,如果陣列的元素是 URL 字串,想要移除重複項目、只保留一個,最快也最直覺的方法是使用 Set。
由於 URL 只要字串字元完全一模一樣就會被視為重複,你可以直接利用 Set 不允許重複值的特性來去重:
1. 最推薦:使用 Set (一行搞定)
Set 是 JavaScript 原生用來儲存唯一值(Unique values)的集合結構。將陣列丟進 Set 後,再利用擴展運算子 ... 轉回陣列即可:
const urls = [
"https://shope.ee",
"https://shope.ee",
"https://shope.ee", // 重複
"https://google.com",
"https://shope.ee" // 重複
];
// 使用 Set 去重
const uniqueUrls = [...new Set(urls)];
console.log(uniqueUrls);
// 輸出: ["https://shope.ee", "https://shope.ee", "https://google.com"]
2. 進階情況:如果網址後面帶有不同的參數(Query Parameters)
如果你的 URL 結尾帶有不同的 ?query=xxx 參數,但你希望「只要主機與路徑一樣,就視為重複並刪除」,可以使用 JavaScript 原生的 URL 物件來解析,並搭配 .filter() 處理:
const urls = [
"https://shope.ee",
"https://shope.ee", // 雖然參數不同,但路徑一樣
"https://shope.ee",
"https://shope.ee" // 路徑一樣
];
const seenPaths = new Set();
const uniqueUrls = urls.filter(urlStr => {
try {
const urlObj = new URL(urlStr);
// urlObj.origin + urlObj.pathname 就是不含參數的純網址路徑
const purePath = urlObj.origin + urlObj.pathname;
if (seenPaths.has(purePath)) {
return false; // 如果這條路徑出現過,就過濾掉(刪除)
}
seenPaths.add(purePath); // 沒出現過,記錄下來
return true; // 保留此元素
} catch (e) {
return false; // 預防格式錯誤的非網址字串
}
});
console.log(uniqueUrls);
// 輸出只保留第一個出現的路徑:
// [ "https://shope.ee", "https://shope.ee" ]
留言
張貼留言