我有一个看起来像这样的 JSON:
var countries = [
{
name: 'united states',
program: {
name: 'usprogram'
}
},
{
name: 'mexico',
program: {
name: 'mexico program'
}
},
{
name: 'panama',
program: [
{
name: 'panama program1'
},
{
name: 'panama program2'
}
]
},
{
name: 'canada'
}
];
有没有办法总是将countries.programs
对象包装到一个数组中,以使最终输出看起来像这样?我尝试了 underscoreJS 中的一些实用功能,但解决方案却让我望而却步。
var countries = [
{
name: 'united states',
program: [ //need to wrap this object into an array
{
name: 'usprogram'
}
]
},
{
name: 'mexico',
program: [ //need to wrap this object into an array
{
name: 'mexico program'
}
]
},
{
name: 'panama',
program: [
{
name: 'panama program1'
},
{
name: 'panama program2'
}
]
},
{
name: 'canada'
}
];
谢谢!