5

I have an array of objects as shown below (although the example below has just one element in the array)

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

Im trying to flatten out each element using underscore.js, so the overall array looks like this:

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

I've got the 'load' element sorted (albeit not generically), since thats just a known 3 field object.

Flattening the cpu array alludes me though. My code is below, along with the output my code is generating

I know I could just write a js loop and be done with it, but Ive seen some very elegant underscore solutions like this, and Im sure its possible. Any advice please?

My Code

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));

My (Wrong) Output

[
    {
        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 Answer 1

10

For example:

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
}    
Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful, Thankyou! I cant tell you how much time I spent messing about with this tonight

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.