admin管理员组文章数量:1277347
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
}
]
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
}
]
Share
Improve this question
asked Oct 28, 2013 at 7:09
carpiicarpii
2,0014 gold badges21 silver badges25 bronze badges
1 Answer
Reset to default 10For 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
}
本文标签: javascriptflattening nested arraysobjects in underscorejsStack Overflow
版权声明:本文标题:javascript - flattening nested arraysobjects in underscore.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741279339a2369915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论