Javascript 作为对象中的值附加到数组

Javascript 作为对象中的值附加到数组,javascript,arrays,json,object,key-value,Javascript,Arrays,Json,Object,Key Value,我返回文件夹对象的结构以及其中包含的json文件的内容。这就是它的样子 { DailyTask: [ { title: 'Chores', body: 'Wash the roof and sweep the bed' } ], monday: [{ title: 'madrid', body: 'Wash the roof and sweep the bed' } ], } 当一个文件夹有两个以上的文件时,我会遇到一个问题,因为我找不到一种方法来附加到monday或DailyTas

我返回文件夹对象的结构以及其中包含的json文件的内容。这就是它的样子

{
  DailyTask: [ { title: 'Chores', body: 'Wash the roof and sweep the bed' } ],
  monday: [{ title: 'madrid', body: 'Wash the roof and sweep the bed' }  ],
}
当一个文件夹有两个以上的文件时,我会遇到一个问题,因为我找不到一种方法来附加到monday或DailyTask的数组中

我尝试过使用concat或push,但它们一开始不起作用,因为对象属性未定义,所以我使用了,第一个赋值将通过方括号完成,后续赋值将通过push或unshift完成,即x是json文件

if (sum[key] == undefined) {
            sum[key] = x;
        } else {
            sum[key].unshift(x);
        }
给我这个

{
  DailyTask: [ { title: 'Chores', body: 'Wash the roof and sweep the bed' } ],
  monday: [
    [ [Object] ],
    { title: 'madrid', body: 'Wash the roof and sweep the bed' }
  ]
}

它显示为[[Object]],如何显示对象的实际内容。

根据您呈现的逻辑
x
等于
[{…}]
。这就是为什么它对我们有用

sum[key] = x;
// outputs:
// title: [ { ... } ]
但另一个失败:
title:[[{…}],{…}]

试试这个:

if(和[键]==未定义){
sum[key]=[x[0]];//或者干脆将其保留为x;
}否则{
求和[key].unshift(x[0]);
}

Ciao,因此您希望创建一个包含两个对象的唯一数组
{title:'Chores',body:'Wash the roof and sweep the bed'}
{title:'madrid',body:'Wash the roof and sweep the bed'}
correct?显示前后的结果。@Sascha第一个代码块和最后一个代码块是results@GiovanniEsposito我想添加更多的格式{title:'XXXXX',body:'Do something xxxxxxx'}yourObject.monday.push({title:'foo});应该有用。你误解了我,我正在推一个格式为{title:'XXX',body:'bla bla bla'}的对象,而不是title:[{…}],{…}的对象,但这正是你得到的输出<代码>[[Object]],{标题:“马德里”,正文:“洗屋顶,扫床”}]。我提供的解决方案仍然不起作用?这不起作用,后续的推送对象显示为[[Object]]对我来说没有意义。什么是
x
console.log
it并查看它是否是这样的:
{“title”:“something”,“body”:“something”}
if(sum[key]==undefined) sum[key]=[x];
else sum[key].push(x);