英文:
remove duplicates from array object - TypeScript
问题
最干净的去重方法是什么。
0: {taxType: 9, taxCode: "a", taxValidFrom: "2020年01月01日 00:00:00.000", taxDesc: "a", …}
1: {taxType: 9, taxCode: "C", taxValidFrom: "2020年01月03日 00:00:00.000", taxDesc: "C", …}
2: {taxType: 9, taxCode: "a", taxValidFrom: "2020年01月04日 00:00:00.000", taxDesc: "a", …}
3: {taxType: 9, taxCode: "C", taxValidFrom: "2020年01月05日 00:00:00.000", taxDesc: "C", …}
4: {taxType: 9, taxCode: "B", taxValidFrom: "2020年01月06日 00:00:00.000", taxDesc: "B", …}
我希望最终得到一个数组,其中每个条目都基于日期和税码。
所以如果税码是C,我应该只有一个,日期为"2020年01月05日 00:00:00.000",因为这是最接近今天日期(2020年01月06日)的条目。
英文:
Whats the cleanest way of removing duplicates.
0: { taxType: 9, taxCode: "a", taxValidFrom: "01 Jan 2020 00:00:00.000", taxDesc: "a", …}
1: { taxType: 9, taxCode: "C", taxValidFrom: "03 Jan 2020 00:00:00.000", taxDesc: "C", …}
2: { taxType: 9, taxCode: "a", taxValidFrom: "04 Jan 2020 00:00:00.000", taxDesc: "a", …}
3: { taxType: 9, taxCode: "C", taxValidFrom: "05 Jan 2020 00:00:00.000", taxDesc: "C", …}
4: { taxType: 9, taxCode: "B", taxValidFrom: "06 Jan 2020 00:00:00.000", taxDesc: "B", …}
I want to end up with an array where there is one entry based on date and taxcode.
So if taxcode is C, i should only have the one where the date is "05 Jan 2020 00:00:00.000", as this is the closest to todays date (06/01/2020)
答案1
得分: 0
你可以循环遍历数组中的每个对象,并推入或替换当前对象。
因此,可以采取以下方法:
- 如果税码在最终数组中不存在,请将新对象推入最终数组。
- 如果税码已经存在于最终数组中,请比较两者中的日期,并保留最新的一个。
建议:
你可以使用 reduce 函数来实现相同的功能。
function modifyArray(arr) {
return arr.reduce((acc, curr) => {
const elementIndexInArray = acc.findIndex(a => a.taxCode === curr.taxCode);
if (elementIndexInArray === -1) {
acc.push(curr);
} else if (new Date(acc[elementIndexInArray].taxValidFrom) < new Date(curr.taxValidFrom)) {
acc.splice(elementIndexInArray, 1, curr);
}
return acc;
}, []);
}
var a = [
{ taxType: 9, taxCode: "a", taxValidFrom: "01 Jan 2020 00:00:00.000", taxDesc: "a" },
{ taxType: 9, taxCode: "C", taxValidFrom: "03 Jan 2020 00:00:00.000", taxDesc: "C" },
{ taxType: 9, taxCode: "a", taxValidFrom: "04 Jan 2020 00:00:00.000", taxDesc: "a" },
{ taxType: 9, taxCode: "C", taxValidFrom: "05 Jan 2020 00:00:00.000", taxDesc: "C" },
{ taxType: 9, taxCode: "B", taxValidFrom: "06 Jan 2020 00:00:00.000", taxDesc: "B" }
];
console.log(modifyArray(a));
(注意:代码部分未翻译。)
英文:
You can loop through each object in the array and push or replace the current object.
So, the approach can be:
- If the taxcode is not present in the final array, push the new object in the final array.
- If the taxcode is already present in the final array, compare the date in both and keep the latest one.
Suggestion:
You can use a reduce function for the same.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function modifyArray(arr) {
return arr.reduce((acc, curr) => {
const elementIndexInArray = acc.findIndex(a => a.taxCode === curr.taxCode);
if(elementIndexInArray === -1) {
acc.push(curr);
} else if (new Date(acc[elementIndexInArray].taxValidFrom) < new Date(curr.taxValidFrom)) {
acc.splice(elementIndexInArray, 1, curr);
}
return acc;
}, []);
}
var a = [
{ taxType: 9, taxCode: "a", taxValidFrom: "01 Jan 2020 00:00:00.000", taxDesc: "a"},
{ taxType: 9, taxCode: "C", taxValidFrom: "03 Jan 2020 00:00:00.000", taxDesc: "C"},
{ taxType: 9, taxCode: "a", taxValidFrom: "04 Jan 2020 00:00:00.000", taxDesc: "a"},
{ taxType: 9, taxCode: "C", taxValidFrom: "05 Jan 2020 00:00:00.000", taxDesc: "C"},
{ taxType: 9, taxCode: "B", taxValidFrom: "06 Jan 2020 00:00:00.000", taxDesc: "B"}
];
console.log(modifyArray(a));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论