我有一个带有对象的动态数组,但似乎无法弄清楚如何在只需要更改某些数据的地方合并/删除对象。
我尝试在两个for循环中遍历代码,但是在保留其他数据的同时无法合并它们。
var record = [
{
client: "Client One",
client_project: "Internal",
project_task: "Other",
project_task_id : '1234a',
day_of_week: "Monday",
hours: "8",
weekDay:{
'Monday':'8',
'Tuesday':'0',
'Wednesday':'0',
'Thursday':'0',
'Friday':'0',
'Saturday':'0',
'Sunday':'0'
}
},
{
client: "Client Two",
client_project: "Client Two Staff Aug #12",
project_task: "Development",
project_task_id : '7284b',
day_of_week: "Tuesday",
hours: "3",
weekDay:{
'Monday':'0',
'Tuesday':'3',
'Wednesday':'0',
'Thursday':'0',
'Friday':'0',
'Saturday':'0',
'Sunday':'0'
}
},
{
client: "Client One",
client_project: "Internal",
project_task: "Other",
project_task_id : '1234a',
day_of_week: "Tuesday",
hours: "5",
weekDay:{
'Monday':'0',
'Tuesday':'5',
'Wednesday':'0',
'Thursday':'0',
'Friday':'0',
'Saturday':'0',
'Sunday':'0'
}
}
]
预期输出如下所示
{
client: "Client One",
client_project: "Internal",
project_task: "Other",
project_task_id : '1234a',
day_of_week: ["Monday", "Tuesday"],
hours: ["8","5"],
weekDay:{
'Monday':'8',
'Tuesday':'5',
'Wednesday':'0',
'Thursday':'0',
'Friday':'0',
'Saturday':'0',
'Sunday':'0'
}
},
{
client: "Client Two",
client_project: "Client Two Staff Aug #12",
project_task: "Development",
project_task_id : '7284b',
day_of_week: "Tuesday",
hours: "3",
weekDay:{
'Monday':'0',
'Tuesday':'3',
'Wednesday':'0',
'Thursday':'0',
'Friday':'0',
'Saturday':'0',
'Sunday':'0'
}
}
]
我认为也许我坚持的是如何遍历所有对象,然后每次都遍历所有对象以构建正确的对象数组。
我一直在玩的是
var newArray = [];
for(var item in record){
if(record[item].project_task_id == record[item++].project_task_id ){
//combine weekDays and remove unnecessary objects
}
}
OR
for(var item in record){
for(var nextItem in record){
if(record[item].project_task_id == record[nextItem].project_task_id ){
//combine weekDays and remove unnecessary objects
}
}
}