检查重复的对象。如果为true,则添加属性并从数组中删除dup

时间:2016-11-30 19:44:50

标签: javascript

此代码有效,但看起来并不干净。还有更好的方法吗?

http://codepen.io/anon/pen/MbOPNL?editors=0012

members = [
    {id: "a0VS0000002H80QMAS", name: "Brett Greathouse", resource: "00530000003mgYGAAY", totalBillableHours: 15, totalHours: 22, totalNonBillableHours: 7},
    {id: "a0VS0000002H7vuMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 10, totalHours: 15, totalNonBillableHours: 5},
    {id: "a0VS0000002H80zMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 5, totalHours: 10, totalNonBillableHours: 5},
    {id: "a0VS0000002H95aMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 4, totalHours: 7, totalNonBillableHours: 3}
]

for (i = 0; i < members.length; i++) {
    var member = members[i];

    if (member.resource == members[i + 1].resource) {

        member.totalHours += members[i + 1].totalHours;
        member.totalBillableHours += members[i + 1].totalBillableHours;
        member.totalNonBillableHours += members[i + 1].totalNonBillableHours;

        members.splice(i + 1, 1);
        --i;
        if (members[i + 2] == undefined) {
            break;
        }

    }

}
console.log(members);

1 个答案:

答案 0 :(得分:0)

您可以从最后进行迭代,因为拼接会更改数组中以下项目的长度和位置。

&#13;
&#13;
var members = [{ id: "a0VS0000002H80QMAS", name: "Brett Greathouse", resource: "00530000003mgYGAAY", totalBillableHours: 15, totalHours: 22, totalNonBillableHours: 7 }, { id: "a0VS0000002H7vuMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 10, totalHours: 15, totalNonBillableHours: 5 }, { id: "a0VS0000002H80zMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 5, totalHours: 10, totalNonBillableHours: 5 }, { id: "a0VS0000002H95aMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 4, totalHours: 7, totalNonBillableHours: 3 }],
    hash = {},
    i = members.length;

while (i--) {
    if (!hash[members[i].resource]) {
        hash[members[i].resource] = members[i];
        continue;
    }
    hash[members[i].resource].totalBillableHours += members[i].totalBillableHours;
    hash[members[i].resource].totalHours += members[i].totalHours;
    hash[members[i].resource].totalNonBillableHours += members[i].totalNonBillableHours;
    members.splice(i, 1);
}

console.log(members);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

另一种解决方案是在不破坏给定数组的情况下创建新数组。

&#13;
&#13;
var members = [{ id: "a0VS0000002H80QMAS", name: "Brett Greathouse", resource: "00530000003mgYGAAY", totalBillableHours: 15, totalHours: 22, totalNonBillableHours: 7 }, { id: "a0VS0000002H7vuMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 10, totalHours: 15, totalNonBillableHours: 5 }, { id: "a0VS0000002H80zMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 5, totalHours: 10, totalNonBillableHours: 5 }, { id: "a0VS0000002H95aMAC", name: "Tyler Zika", resource: "005a000000C2O2KAAV", totalBillableHours: 4, totalHours: 7, totalNonBillableHours: 3 }],
    grouped = [];

members.forEach(function (a) {
    if (!this[a.resource]) {
        this[a.resource] = { id: a.id, name: a.name, resource: a.resource, totalBillableHours: 0, totalHours: 0, totalNonBillableHours: 0 };
        grouped.push(this[a.resource]);
    }
    this[a.resource].totalBillableHours += a.totalBillableHours;
    this[a.resource].totalHours += a.totalHours;
    this[a.resource].totalNonBillableHours += a.totalNonBillableHours;
}, Object.create(null));

console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;