从数组对象中移除重复项 – TypeScript

huangapple go评论62阅读模式
英文:

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

你可以循环遍历数组中的每个对象,并推入或替换当前对象。

因此,可以采取以下方法:

  1. 如果税码在最终数组中不存在,请将新对象推入最终数组。
  2. 如果税码已经存在于最终数组中,请比较两者中的日期,并保留最新的一个。

建议:
你可以使用 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:

  1. If the taxcode is not present in the final array, push the new object in the final array.
  2. 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) =&gt; {
        const elementIndexInArray = acc.findIndex(a =&gt; a.taxCode === curr.taxCode);
        if(elementIndexInArray === -1) {
            acc.push(curr);
        } else if (new Date(acc[elementIndexInArray].taxValidFrom) &lt; new Date(curr.taxValidFrom)) {
            acc.splice(elementIndexInArray, 1, curr);
        }
    
        return acc;
    }, []);
}

var a = [
    { taxType: 9, taxCode: &quot;a&quot;, taxValidFrom: &quot;01 Jan 2020 00:00:00.000&quot;, taxDesc: &quot;a&quot;},
    { taxType: 9, taxCode: &quot;C&quot;, taxValidFrom: &quot;03 Jan 2020 00:00:00.000&quot;, taxDesc: &quot;C&quot;},
    { taxType: 9, taxCode: &quot;a&quot;, taxValidFrom: &quot;04 Jan 2020 00:00:00.000&quot;, taxDesc: &quot;a&quot;},
    { taxType: 9, taxCode: &quot;C&quot;, taxValidFrom: &quot;05 Jan 2020 00:00:00.000&quot;, taxDesc: &quot;C&quot;},
    { taxType: 9, taxCode: &quot;B&quot;, taxValidFrom: &quot;06 Jan 2020 00:00:00.000&quot;, taxDesc: &quot;B&quot;}
];

console.log(modifyArray(a));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 01:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定