5

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

[
    {
        "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,
    }
]

我已经对“加载”元素进行了排序(尽管不是一般性的),因为那只是一个已知的 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
    }
]
4

1 回答 1

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
}    
于 2013-10-28T07:55:43.780 回答