所以,我有一个包含多个对象的数组。
var profile = [
{"MODE":"Comfort","MONDAY":"09:00:00","TUESDAY":"09:00:00","WEDNESDAY":"09:00:00",
"THURSDAY":"09:00:00","FRIDAY":"09:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Eco","MONDAY":"22:00:00","TUESDAY":"22:00:00","WEDNESDAY":"22:00:00",
"THURSDAY":"22:00:00","FRIDAY":"22:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"07:30:00","TUESDAY":"07:30:00","WEDNESDAY":"07:30:00",
"THURSDAY":"07:30:00","FRIDAY":"07:30:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"21:00:00","TUESDAY":"21:00:00","WEDNESDAY":"21:00:00",
"THURSDAY":"21:00:00","FRIDAY":"21:00:00","SATURDAY":null,"SUNDAY":null}
];
有一个MODE属性和一个工作日属性。
我想要的是将所有MONDAY值存储在称为MONDAY的数组中,所有TUESDAY值存储在称为TUESDAY的数组中,等等。该值的键必须为MODE(舒适度,eco等)的值。 这就是目标:
var MONDAY = ["Comfort":"09:00:00","ECO":"22:00:00", ...]
var TUESDAY = ["Comfort":"09:00:00","ECO":"22:00:00",...]
我尝试过滤初始数组
var comfort = profile.filter(p => p.MODE == "Comfort");
var eco = profile.filter(p => p.MODE == "Eco");
var standby = profile.filter(p => p.MODE == "Standby");
var comfortMode = comfort[0];
var ecoMode = eco[0];
var standbyMode0 = standby[0];
var standbyMode1 = standby[1];
console.log(ecoMode["MONDAY"]);
console.log(standbyMode0["MONDAY"]);
但是我在这一点上陷入了困境。似乎我需要对值进行硬编码,并手动编写
var MONDAY = [ecoMode["MONDAY"],comfortMode["MONDAY"],standbyMode0["MONDAY"],standbyMode1["MONDAY"]];
var TUESDAY = [...,...,...,...]
是否有任何方法可以为comfortMode,ecoMode和其他数组创建循环,而每个键为MONDAY的值都存储在名为MONDAY的数组中?
答案 0 :(得分:0)
这是您可以使用的解决方案。它不是最漂亮,但可以正常工作,并且可以帮助您入门
var profile = [
{"MODE":"Comfort","MONDAY":"09:00:00","TUESDAY":"09:00:00","WEDNESDAY":"09:00:00",
"THURSDAY":"09:00:00","FRIDAY":"09:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Eco","MONDAY":"22:00:00","TUESDAY":"22:00:00","WEDNESDAY":"22:00:00",
"THURSDAY":"22:00:00","FRIDAY":"22:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"07:30:00","TUESDAY":"07:30:00","WEDNESDAY":"07:30:00",
"THURSDAY":"07:30:00","FRIDAY":"07:30:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"21:00:00","TUESDAY":"21:00:00","WEDNESDAY":"21:00:00",
"THURSDAY":"21:00:00","FRIDAY":"21:00:00","SATURDAY":null,"SUNDAY":null}
];
let monday = []
let tuesday = []
let wednesday = []
let thursday = []
let friday = []
let saturday = []
let sunday = []
profile.forEach(e => {
let mode = e.MODE
let mondayVal = e.MONDAY
let tuesdayVal = e.TUESDAY
let wednesdayVal = e.WEDNESDAY
let thursdayVal = e.THURSDAY
let fridayVal = e.FRIDAY
let saturdayVal = e.SATURDAY
let sundayVal = e.SUNDAY
monday.push({[mode]: mondayVal})
tuesday.push({[mode]: tuesdayVal})
wednesday.push({[mode]: wednesdayVal})
thursday.push({[mode]: thursdayVal})
friday.push({[mode]: fridayVal})
saturday.push({[mode]: saturdayVal})
sunday.push({[mode]: sundayVal})
})
console.log("Monday: ", monday)
console.log("Tuesday: ", tuesday)
console.log("Wednesday: ", wednesday)
console.log("Thursday: ", thursday)
console.log("Friday: ", friday)
console.log("Saturday: ", saturday)
console.log("Sunday: ", sunday)
如果您有任何疑问,请告诉我。
答案 1 :(得分:0)
您可以执行此操作以每天获得一小时的时间
let profile = [
{"MODE":"Comfort","MONDAY":"09:00:00","TUESDAY":"09:00:00","WEDNESDAY":"09:00:00",
"THURSDAY":"09:00:00","FRIDAY":"09:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Eco","MONDAY":"22:00:00","TUESDAY":"22:00:00","WEDNESDAY":"22:00:00",
"THURSDAY":"22:00:00","FRIDAY":"22:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"07:30:00","TUESDAY":"07:30:00","WEDNESDAY":"07:30:00",
"THURSDAY":"07:30:00","FRIDAY":"07:30:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"21:00:00","TUESDAY":"21:00:00","WEDNESDAY":"21:00:00",
"THURSDAY":"21:00:00","FRIDAY":"21:00:00","SATURDAY":null,"SUNDAY":null}
];
// declare the object with the resulting arrays
let days = {}
profile.forEach(prof => {
// we remember the mode for later
let mode = prof.MODE;
// for .. in loop each attribute of an object
for (let i in prof) {
// not needed to save modes
if (i !== "MODE") {
// create the array for the day if it doesn't exists
if (typeof days[i] === "undefined") {days[i] = [];}
// add the current value
days[i].push(prof[i]);
}
}
})
// show the final object
console.log(days)
或者这仍然具有模式
let profile = [
{"MODE":"Comfort","MONDAY":"09:00:00","TUESDAY":"09:00:00","WEDNESDAY":"09:00:00",
"THURSDAY":"09:00:00","FRIDAY":"09:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Eco","MONDAY":"22:00:00","TUESDAY":"22:00:00","WEDNESDAY":"22:00:00",
"THURSDAY":"22:00:00","FRIDAY":"22:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"07:30:00","TUESDAY":"07:30:00","WEDNESDAY":"07:30:00",
"THURSDAY":"07:30:00","FRIDAY":"07:30:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"21:00:00","TUESDAY":"21:00:00","WEDNESDAY":"21:00:00",
"THURSDAY":"21:00:00","FRIDAY":"21:00:00","SATURDAY":null,"SUNDAY":null}
];
// declare the object with the resulting arrays
let days = {}
profile.forEach(prof => {
// we remember the mode for later
let mode = prof.MODE;
// for .. in loop each attribute of an object
for (let i in prof) {
// not needed to save modes
if (i !== "MODE") {
// create the object for the day if it doesn't exists
if (typeof days[i] === "undefined") {days[i] = {};}
// create the array for the mode if it doesn't exists
if (typeof days[i][mode] === "undefined") {days[i][mode] = []}
// add the current value
days[i][mode].push(prof[i]);
}
}
})
// show the final object
console.log(days)
这两种方法使您可以毫无问题地扩展初始数组
答案 2 :(得分:0)
这里是您的问题的解决方案。唯一的不同是,在最终输出中,您将不会:
var MONDAY = ["Comfort":"09:00:00","ECO":"22:00:00", ...]
var TUESDAY = ["Comfort":"09:00:00","ECO":"22:00:00",...]
它将是一个以工作日为键的对象。
{
"MONDAY": [
{
"Comfort": "09:00:00"
},
{
"Eco": "22:00:00"
},
{
"Standby": "07:30:00"
},
{
"Standby": "21:00:00"
}
],
"TUESDAY": [
{
"Comfort": "09:00:00"
},
{
"Eco": "22:00:00"
},
{
"Standby": "07:30:00"
},
{
"Standby": "21:00:00"
}
],
"WEDNESDAY": [
{
"Comfort": "09:00:00"
},
{
"Eco": "22:00:00"
},
{
"Standby": "07:30:00"
},
{
"Standby": "21:00:00"
}
],
"THURSDAY": [
{
"Comfort": "09:00:00"
},
{
"Eco": "22:00:00"
},
{
"Standby": "07:30:00"
},
{
"Standby": "21:00:00"
}
],
"FRIDAY": [
{
"Comfort": "09:00:00"
},
{
"Eco": "22:00:00"
},
{
"Standby": "07:30:00"
},
{
"Standby": "21:00:00"
}
],
"SATURDAY": [
{
"Comfort": null
},
{
"Eco": null
},
{
"Standby": null
},
{
"Standby": null
}
],
"SUNDAY": [
{
"Comfort": null
},
{
"Eco": null
},
{
"Standby": null
},
{
"Standby": null
}
]
}
var profile = [
{"MODE":"Comfort","MONDAY":"09:00:00","TUESDAY":"09:00:00","WEDNESDAY":"09:00:00",
"THURSDAY":"09:00:00","FRIDAY":"09:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Eco","MONDAY":"22:00:00","TUESDAY":"22:00:00","WEDNESDAY":"22:00:00",
"THURSDAY":"22:00:00","FRIDAY":"22:00:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"07:30:00","TUESDAY":"07:30:00","WEDNESDAY":"07:30:00",
"THURSDAY":"07:30:00","FRIDAY":"07:30:00","SATURDAY":null,"SUNDAY":null},
{"MODE":"Standby","MONDAY":"21:00:00","TUESDAY":"21:00:00","WEDNESDAY":"21:00:00",
"THURSDAY":"21:00:00","FRIDAY":"21:00:00","SATURDAY":null,"SUNDAY":null}
];
var weekdays = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"];
var finalResult = weekdays.reduce((acc, currentDay) => {
var dayValues = profile.reduce((acc1, currentProfile) => {
return [...acc1, {
[currentProfile.MODE]: currentProfile[currentDay]
}]
}, []);
return {
...acc,
[currentDay]: dayValues,
};
}, {});
console.log(finalResult);
答案 3 :(得分:0)
您可以使用Array.prototype.reduce
对输入数组中的项目进行分组并汇总所需的输出:
var profile=[{MODE:"Comfort",MONDAY:"09:00:00",TUESDAY:"09:00:00",WEDNESDAY:"09:00:00",THURSDAY:"09:00:00",FRIDAY:"09:00:00",SATURDAY:null,SUNDAY:null},{MODE:"Eco",MONDAY:"22:00:00",TUESDAY:"22:00:00",WEDNESDAY:"22:00:00",THURSDAY:"22:00:00",FRIDAY:"22:00:00",SATURDAY:null,SUNDAY:null},{MODE:"Standby",MONDAY:"07:30:00",TUESDAY:"07:30:00",WEDNESDAY:"07:30:00",THURSDAY:"07:30:00",FRIDAY:"07:30:00",SATURDAY:null,SUNDAY:null},{MODE:"Standby",MONDAY:"21:00:00",TUESDAY:"21:00:00",WEDNESDAY:"21:00:00",THURSDAY:"21:00:00",FRIDAY:"21:00:00",SATURDAY:null,SUNDAY:null}];
var result = profile.reduce((all, el) => {
Object.keys(el).filter(k => k !== 'MODE').forEach(k => all[k].push({[el.MODE]: el[k]}));
return all;
}, { MONDAY:[], TUESDAY:[], WEDNESDAY: [], THURSDAY:[], FRIDAY:[], SATURDAY:[], SUNDAY:[] });
console.log(result);