json对象是
var data = [{"Parent":1,"Child":[4,5,6]},{"Parent":2},{"Parent":3}]
如何使用 underscore.js chain/map/pluck 等函数来获得扁平化结果
var result = [];
for (var i = 0; i < data.length; i++) {
result.push(data[i].Parent);
if (data.Child != undefined) {
for (var j = 0; j < data[i].Child.length; j++) {
result.push(data[i].Child[j]);
}
}
}
console.log(result) >> //1,4,5,6,2,3
最佳答案
这是一个更简短的解决方案:
flat = _.flatten(_.map(data, _.values))
关于javascript - 如何使用 underscore.js 生成扁平化结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8786417/