使用Lodash转换对象数组
Transforming array of objects using Lodash
我试图简化一个复杂的结构。
我已经尽可能地简化了它,并将其简化为:
[
{'Cells':{'Results':[
{'Key':'ID','Value':123},
{'Key':'Color','Value':'Red'},
{'Key':'Direction','Value':'West'}
]}},
{'Cells':{'Results':[
{'Key':'ID','Value':456},
{'Key':'Color','Value':'Green'},
{'Key':'Direction','Value':'East'}
]}}
]
我的lodash技能不足,我需要帮助把上面的变成这个:
[
{'ID':123, 'Color':'Red', 'Direction':'West'},
{'ID':456, 'Color':'Green', 'Direction':'East'}
]
顺便说一下,键的数量因对象而异。至少,它可以只有ID,但在示例中,有些可能有超过三个。
在普通Javascript中,您可以为它使用两个嵌套循环。
var array = [{ 'Cells': { 'Results': [{ 'Key': 'ID', 'Value': 123 }, { 'Key': 'Color', 'Value': 'Red' }, { 'Key': 'Direction', 'Value': 'West' }] } }, { 'Cells': { 'Results': [{ 'Key': 'ID', 'Value': 456 }, { 'Key': 'Color', 'Value': 'Green' }, { 'Key': 'Direction', 'Value': 'East' }] } }],
condensed = array.map(function (a) {
var o = {};
a.Cells.Results.forEach(function (b) {
o[b.Key] = b.Value;
});
return o;
});
console.log(condensed);
使用Lodash:
var result = _.chain(data)
.map('Cells.Results')
.map(res => _.zipObject(_.map(res,"Key"), _.map(res,"Value")))
.value();
应该这样做(尽管我不确定它是否比纯javascript更好)
_.chain(array)
.pluck('Cells')
.pluck('Results')
.map(function (value) { return _.object(_.map(value, _.values)); })
.value()
这是使用lodash fp的另一种选择。
var result = map(compose(
mapValues('Value'),
keyBy('Key'),
iteratee('Cells.Results')
))(data);
var data = [{
'Cells': {
'Results': [{
'Key': 'ID',
'Value': 123
}, {
'Key': 'Color',
'Value': 'Red'
}, {
'Key': 'Direction',
'Value': 'West'
}]
}
}, {
'Cells': {
'Results': [{
'Key': 'ID',
'Value': 456
}, {
'Key': 'Color',
'Value': 'Green'
}, {
'Key': 'Direction',
'Value': 'East'
}]
}
}];
var { compose, map, iteratee, keyBy, mapValues } = _;
var result = map(compose(
mapValues('Value'),
keyBy('Key'),
iteratee('Cells.Results')
))(data);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.fp.js"></script>
又是一个答案,使用map和reduce
var initial_array=[
{'Cells':{'Results':[
{'Key':'ID','Value':123},
{'Key':'Color','Value':'Red'},
{'Key':'Direction','Value':'West'}
]}},
{'Cells':{'Results':[
{'Key':'ID','Value':456},
{'Key':'Color','Value':'Green'},
{'Key':'Direction','Value':'East'}
]}}
];
function mergeObj(accumulator,current){
accumulator[current['Key']]=current['Value'];
return accumulator;
}
function getResults(obj){
return obj.Cells.Results.reduce(mergeObj,{});
}
var transformed_array=_(initial_array).map(getResults).value();
console.log(transformed_array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
相关文章:
- 将数组对象传递到struts2中的操作类
- 如何使用jquery返回php-json数组对象
- 从其名称获取javascript数组对象
- 值未与数组对象绑定
- 如何按数组/对象值的倍数过滤对象数组
- 在表中显示数组对象
- 在数组对象中分组
- 将第二个或多个数组推送到数组对象 AngularJS 中
- 重新排列 JavaScript 数组/对象
- 键上的javascript数组对象过滤器
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 用于跟踪购物车可用性的Javascript数组/对象/哈希表
- 重建有角度的java脚本数组对象
- 对JSON数组对象进行排序
- 连接与数组对象相关的文本:方式和位置
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 如何使用Handlebars循环数组对象和模板
- 是否可以引用JS数组/对象中的另一个元素
- Javascript中的名称索引-数组/对象
- 数组长度不等于数组对象