我從 api 收到以下資料結構:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
]
清理代碼的完美結果將是這個結果,基本上只回傳第一個找到的 id:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
]
但目前它只回傳唯一陣列中最后找到的重復項,使用當前代碼:
let uniqueArray = [...new Map(data.map(item =>
[item.id, item])).values()];
uj5u.com熱心網友回復:
您可以使用Object.values()和reduce()相同:
const data = [
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
]
const result = Object.values(
data.reduce((res, {id, text_id}) => {
res[id] ??= {id, text_id}
return res
}, {})
)
console.log(result)
更新檔案_??=
邏輯空賦值(x ??= y)運算子只賦值if x is nullish (null or undefined)。
uj5u.com熱心網友回復:
你可以這樣做:
const arr = [
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
];
const unique = [];
const visited = new Set();
for(let i = 0; i < arr.length; i){
if(!visited.has(arr[i].id)){
unique.push(arr[i]);
visited.add(arr[i].id);
}
}
console.log(unique);
uj5u.com熱心網友回復:
概念
回圈遍歷資料中的所有物件并檢查是否在結果陣列中找到了 id。如果沒有找到,則將其推入結果陣列;否則,跳過它。
代碼
const data = [{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
];
let result = [];
let found;
data.forEach(d => {
found = false;
result.forEach(r => {
if (!found && r.id === d.id) found = true;
})
if (!found) result.push(d);
});
console.log(result);
uj5u.com熱心網友回復:
這是實作您想要的輸出的另一種方法。
我們reduce用于更改資料并使用find函式來獲取當前id是否已經存在,prev如果not然后我們使用filter函式過濾掉相同的id物件并僅首先推送。
const data =[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
]
const newData = data.reduce((prev, curr, index, arr) => {
const find = prev.find(p => p.id === curr.id);
if (!find) {
const filter = arr.filter(f => f.id === curr.id);
if (filter.length >= 1) {
prev.push(filter[0])
}
}
return prev;
}, [])
console.log(newData)
uj5u.com熱心網友回復:
您可以reduce將陣列轉換為一個物件,該物件僅存盤每個 id 的第一次出現,然后values從該物件中提取。
const
data = [{ id: "10000844", text_id: "10000844-01" }, { id: "10000844", text_id: "10000844-02" }, { id: "12000844", text_id: "12000844-03" }, { id: "12000844", text_id: "12000844-07" }, { id: "12000814", text_id: "12000844-07" }, { id: "12002812", text_id: "12000844-07" }, { id: "12000814", text_id: "12000844-08" }],
result = Object.values(data.reduce((acc, d) => (!acc[d.id] ? { ...acc, [d.id]: d } : acc), {}));
console.log(result);
uj5u.com熱心網友回復:
有些答案已經是正確的。
此答案僅適用于您使用 LODASH
您可以使用uniqBy。
這將回傳一個無重復版本的陣列,其中第一次出現的就是將被保留的那個:
所以在你的情況下,像
import uniqBy from 'lodash/uniqBy';
// dataFromAPI is your sample data
const unqiueArray = uniqBy(dataFromAPI, 'id');
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497305.html
標籤:javascript 数组 字典 目的 独特的
下一篇:持續時間不適用于所有行
