javascript - 合并新对象中的对象属性并插入数组,无需 jquery 和 lodash

标签 javascript arrays data-structures

我有一组具有相同属性的对象。如何将具有相同 period 属性的对象合并到新的对象数组中。我在 stackoverflow 中看到了示例,但它们大多使用 JQuery 或 lodash 等外部库。我可以用 vanilla.js 做这个吗?

<强> Here 是带有代码和期望值标记的 Plunkr。谢谢您的帮助

最佳答案

您可以将其与日期分组并使用对象作为对哈希表的引用。另一个对象用于访问 day/nicht 属性。

var array = [{ period: '01.07.2016', count: 11, scale: 'd' }, { period: '01.07.2016', count: 22, scale: 'n' }, { period: '01.08.2016', count: 33, scale: 'n' }, { period: '01.08.2016', count: 44, scale: 'd' }, { period: '01.09.2016', count: 55, scale: 'd' }, { period: '01.09.2016', count: 66, scale: 'n' }],
    grouped = [];

array.forEach(function (a) {
    var day = a.period.slice(3, 5);
    if (!this[day]) {
        this[day] = { date: day, day: 0, night: 0 };
        grouped.push(this[day]);
    }
    this[day][{ d: 'day', n: 'night' }[a.scale]] += a.count;
}, Object.create(null));

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

您也可以在哈希表上使用闭包,而不使用 thisArg

var array = [{ period: '01.07.2016', count: 11, scale: 'd' }, { period: '01.07.2016', count: 22, scale: 'n' }, { period: '01.08.2016', count: 33, scale: 'n' }, { period: '01.08.2016', count: 44, scale: 'd' }, { period: '01.09.2016', count: 55, scale: 'd' }, { period: '01.09.2016', count: 66, scale: 'n' }],
    grouped = [];

array.forEach(function (hash) {
    return function (a) {
        var day = a.period.slice(3, 5);
        if (!hash[day]) {
            hash[day] = { date: day, day: 0, night: 0 };
            grouped.push(hash[day]);
        }
        hash[day][{ d: 'day', n: 'night' }[a.scale]] += a.count;
    };
}(Object.create(null)));

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

没有滥用 thisArg 和 IFFE 的干净版本

var array = [{ period: '01.07.2016', count: 11, scale: 'd' }, { period: '01.07.2016', count: 22, scale: 'n' }, { period: '01.08.2016', count: 33, scale: 'n' }, { period: '01.08.2016', count: 44, scale: 'd' }, { period: '01.09.2016', count: 55, scale: 'd' }, { period: '01.09.2016', count: 66, scale: 'n' }],
    grouped = function () {
        var result = [],
            hash = Object.create(null);

        array.forEach(function (a) {
            var day = a.period.slice(3, 5);
            if (!hash[day]) {
                hash[day] = { date: day, day: 0, night: 0 };
                result.push(hash[day]);
            }
            hash[day][{ d: 'day', n: 'night' }[a.scale]] += a.count;
        });
        return result;
    }();

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

关于javascript - 合并新对象中的对象属性并插入数组,无需 jquery 和 lodash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718857/

相关文章:

c - 在 C 编程中是否可以让 for 循环遍历多个数组/变量?

php - 在PHP中的每个级别递归排序多维数组

c++ - 如何使用常数时间(O(1))计算链表的长度

algorithm - 构造后缀树线性的最坏情况时间复杂度如何?

java - 在Java中保存集合最有效的方法是什么?

javascript - 使用 YUI 或 Ext JS 将隐藏的 DIV 变成一个窗口

javascript - UI Bootstrap 选项卡在重新加载后保存事件选项卡

ios - 如何将一组(数组)图像保存到firebase云存储中?

javascript - 使用 jquery ajax 避免整个页面闪烁

javascript - knockout 绑定(bind)无法按预期操作可观察数组