在underscore.js中展平嵌套数组/对象

时间:2013-10-28 07:09:31

标签: javascript node.js underscore.js

我有一个对象数组,如下所示(尽管下面的例子只有数组中的一个元素)

[
    {
        "uptime":0,
        "load":{"x":0.11,"y":0.22,"z":0.33},
        "cpu":[
            {"u":111,"n":112,"s":113,"i":114,"q":115}, 
            {"u":211,"n":212,"s":213,"i":214,"q":215}
        ]
    }
]

我试图使用underscore.js来展平每个元素,所以整个数组看起来像这样:

[
    {
        "uptime":0,

        "load_x": 0.11
        "load_y": 0.03
        "load_z": 0.01,

        "cpu1_u": 111,
        "cpu1_n": 112,
        "cpu1_s": 113,
        "cpu1_i": 114,
        "cpu1_q": 115,

        "cpu2_u": 211,
        "cpu2_n": 212,
        "cpu2_s": 213,
        "cpu2_i": 214,
        "cpu2_q": 215,
    }
]

我已经对'load'元素进行了排序(虽然不是一般),因为那只是一个已知的3个字段对象。

扁平化cpu阵列虽然暗示了我。 我的代码如下,以及我的代码生成的输出

我知道我可以写一个js循环并完成它,但我已经看到了一些非常优雅的下划线解决方案,我确信它可能。 有什么建议吗?

我的代码

var profiles = [
    {
        "uptime":0,
        "load":{"x":0.11,"y":0.22,"z":0.33},
        "cpu":[
            {"u":111,"n":112,"s":113,"i":114,"q":115}, 
            {"u":211,"n":212,"s":213,"i":214,"q":215}
        ]
    }
];

var flat = _.map(profiles, function(profile) {
        var p = _.extend(_.omit(profile, 'load'), {
            load_1: Math.round(100*profile.load.x)/100, 
            load_5: Math.round(100*profile.load.y)/100, 
            load_15: Math.round(100*profile.load.z)/100
        });

        var cpuid = 0;
        var cpuobject =
            _.map(p.cpu, function(cpu) {
                cpuid++;
                return _.object(
                    _.map(cpu, function(val, key) {
                        var arr = ['cpu'+cpuid+'_'+key, val];
                        return arr;
                    })
                );
            });
        return _.extend(_.omit(p, 'cpu'), cpuobject);
    });

console.log(JSON.stringify(flat));

我的(错误)输出

[
    {
        0: {
            cpu1_u: 233264700,
            cpu1_n: 0,
            cpu1_s: 64485200,
            cpu1_i: 1228073616,
            cpu1_q: 86100
        },
        1: {
            cpu2_u: 233264700,
            cpu2_n: 0,
            cpu2_s: 64485200,
            cpu2_i: 1228073616,
            cpu2_q: 86100
        },
        uptime: 0,
        load_1: 0.11,
        load_5: 0.03,
        load_15: 0.01
    }
]

1 个答案:

答案 0 :(得分:10)

例如:

flatten = function(x, result, prefix) {
    if(_.isObject(x)) {
        _.each(x, function(v, k) {
            flatten(v, result, prefix ? prefix + '_' + k : k)
        })
    } else {
        result[prefix] = x
    }
    return result
}


a =
{
    "uptime":0,
    "load":{"x":0.11,"y":0.22,"z":0.33},
    "cpu":[
        {"u":111,"n":112,"s":113,"i":114,"q":115},
        {"u":211,"n":212,"s":213,"i":214,"q":215}
    ]
}


result = flatten(a, {})

{
        "uptime": 0,
        "load_x": 0.11,
        "load_y": 0.22,
        "load_z": 0.33,
        "cpu_0_u": 111,
        "cpu_0_n": 112,
        "cpu_0_s": 113,
        "cpu_0_i": 114,
        "cpu_0_q": 115,
        "cpu_1_u": 211,
        "cpu_1_n": 212,
        "cpu_1_s": 213,
        "cpu_1_i": 214,
        "cpu_1_q": 215
}