数据来源
var data = {
"2017-08-09": [
{
"time": "09:00",
"available": true
},
{
"time": "13:00",
"available": true
},
{
"time": "13:30",
"available": true
},
]
}
期望的转型
newData = [ "2017-08-09": ['09:00','13:00','13:30'] ]
我尝试了以下内容:
function recursiveFunction(collection){
var newData = [];
_.forOwn(collection, function(value, key){
newData.push( {key});
value.map( item => {
if (item.available === true) newData.key = item.time
})
});
console.log(newData)
};
但不完全存在:/
答案 0 :(得分:2)
使用mapValues,filter仅使用available
次来映射值,并使用map拨打时间:
var newData = _.mapValues(data, times => _.map(_.filter(times, 'available'), 'time'));
答案 1 :(得分:1)
使用vanilla JS,使用Object#keys获取一系列密钥,并使用Array#map,Array#filter和computed property names将其转换为对象:
var data = {"2017-08-09":[{"time":"09:00","available":true},{"time":"09:30","available":false},{"time":"11:30","available":true}],"2017-08-10":[{"time":"10:00","available":true},{"time":"10:30","available":false}]};
var result = Object.keys(data).map(function(key) {
return {
[key]: data[key]
.filter(function(o) {
return o.available;
})
.map(function(o) {
return o.time;
})
};
});
console.log(result);