Javascript,从对象中删除所有,真正的所有重复项
Javasript, remove ALL, REALLY ALL duplicates from object
有带有注释、时间条目的对象,想要删除所有(所有,抱歉重复这部分,我已经经历了很多例子,它们都只删除第二个、第三个等重复项,但它们包括第一个重复项)与它同时重复的笔记。
好吧,我需要将唯一项与重复项分开,我已经使用我在 Whosebug 上找到的这段代码成功地从中获取了所有重复项,
const allNotes = [
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
}
]
const getDuplicates = () => {
const values = allNotes;
const unique = new Object;
const lookup = values.reduce((a, e) => {
a[e.time] = ++a[e.time] || 0;
return a;
}, {});
const duplicates = values.filter(e => lookup[e.time]);
console.log(duplicates);
}
这段代码就像它产生的魅力一样工作
[
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
}
]
仍然需要能够只获取唯一的时间条目,需要这个结果
[
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
}
]
我猜你正在寻找这样的东西:
array.filter(a => array.filter(b => a.time === b.time).length === 1)
// filter array and sort for duplicates, just keep element if time-property is unique
请参阅下面的工作示例:
const allNotes=[{note:69,time:0},{note:57,time:0},{note:60,time:1.5},{note:64,time:2},{note:69,time:2.5},{note:71,time:3},{note:52,time:3},{note:64,time:4.5},{note:68,time:5},{note:71,time:5.5}];
const res = allNotes.filter(a => allNotes.filter(b => a.time === b.time).length === 1);
console.log(res);
您已经解决了您的问题,因为您能够找出重复项。过滤相反的就可以了。
const uniqueValues = values.filter(e => !lookup[e.time]);
const _dedupArr: any[] = [];
allNotes.forEach((v, i) => {
const find = _dedupArr.find(d => d.time == v.time);
if(!find) {
_dedupArr.push(v);
}
});
console.log('_dedupArr', _dedupArr);
不是一个高效的解决方案,但它绝对是一个简单的解决方案...将每个对象转换为基元,然后可以将这些基元分配给地图,并且只能检索值。
const allNotes = [
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
},
];
const uniqueMap = new Map();
allNotes.forEach(value => uniqueMap.set(
// Any conversion to primitive is fine, JSON.stringify too
`${value.note}-${value.time}`,
value,
));
console.log(Array.from(uniqueMap.values()));
有带有注释、时间条目的对象,想要删除所有(所有,抱歉重复这部分,我已经经历了很多例子,它们都只删除第二个、第三个等重复项,但它们包括第一个重复项)与它同时重复的笔记。
好吧,我需要将唯一项与重复项分开,我已经使用我在 Whosebug 上找到的这段代码成功地从中获取了所有重复项,
const allNotes = [
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
}
]
const getDuplicates = () => {
const values = allNotes;
const unique = new Object;
const lookup = values.reduce((a, e) => {
a[e.time] = ++a[e.time] || 0;
return a;
}, {});
const duplicates = values.filter(e => lookup[e.time]);
console.log(duplicates);
}
这段代码就像它产生的魅力一样工作
[
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
}
]
仍然需要能够只获取唯一的时间条目,需要这个结果
[
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
}
]
我猜你正在寻找这样的东西:
array.filter(a => array.filter(b => a.time === b.time).length === 1)
// filter array and sort for duplicates, just keep element if time-property is unique
请参阅下面的工作示例:
const allNotes=[{note:69,time:0},{note:57,time:0},{note:60,time:1.5},{note:64,time:2},{note:69,time:2.5},{note:71,time:3},{note:52,time:3},{note:64,time:4.5},{note:68,time:5},{note:71,time:5.5}];
const res = allNotes.filter(a => allNotes.filter(b => a.time === b.time).length === 1);
console.log(res);
您已经解决了您的问题,因为您能够找出重复项。过滤相反的就可以了。
const uniqueValues = values.filter(e => !lookup[e.time]);
const _dedupArr: any[] = [];
allNotes.forEach((v, i) => {
const find = _dedupArr.find(d => d.time == v.time);
if(!find) {
_dedupArr.push(v);
}
});
console.log('_dedupArr', _dedupArr);
不是一个高效的解决方案,但它绝对是一个简单的解决方案...将每个对象转换为基元,然后可以将这些基元分配给地图,并且只能检索值。
const allNotes = [
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
},
];
const uniqueMap = new Map();
allNotes.forEach(value => uniqueMap.set(
// Any conversion to primitive is fine, JSON.stringify too
`${value.note}-${value.time}`,
value,
));
console.log(Array.from(uniqueMap.values()));