我有一个像这样的JSON对象:
{
"Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)":[
{ ... },
{ ... }
],
"Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)":[
{ ... }
],
"Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)":[
{ ... }
]
}
如何按月将其返回到分组对象中?
答案 0 :(得分:0)
尝试此操作(带破折号):
$('#password, #confirm_password').on('keyup', function () {
if ($('#password').val() == $('#confirm_password').val()) {
$('#message').html('Matching').css('color', 'green');
$("#submit").prop('disabled',false);
} else {
$('#message').html('Not Matching').css('color', 'red');
$("#submit").prop('disabled',true);
}
});
输出:
var input = {
"Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)": [
"test", "tester"
],
"Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)": [
"tester1"
],
"Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)": [
"tester2"
]
}
var groupedKeys = _.groupBy(Object.keys(input), function(key) {
return key.substring(4, 7)
});
var groupedEntries = _.map(groupedKeys, (values, key) => {
values = _.map(values, (value) => {
return input[value]
});
return {
key: key,
values: _.flattenDeep(values)
};
});
var result = _.reduce(groupedEntries, (result, obj) => {
result[obj.key] = obj.values;
return result;
}, {})
console.log(result);
答案 1 :(得分:0)
浏览对象,然后提取月份并创建新对象:
let jsonObj = {
"Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)":[{},{}],
"Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)":[{}],
"Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)":[{}]
};
let grouped = Object.keys(jsonObj).reduce((prev,key,i,all)=>{
let monthArr = {};
if(typeof prev === 'string'){
monthArr[prev.substr(4,3)] = jsonObj[prev];
prev = {};
}
monthArr[key.substr(4,3)] = jsonObj[key];
return {...prev,...monthArr};
});