作为对象中的值追加到数组
append to an array as a value in an object
我正在返回文件夹对象的结构以及其中包含的 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] == undefined) {
sum[key] = [x[0]] ; // or simply leave it x;
} else {
sum[key].unshift(x[0]);
}
if(sum[key]==undefined) sum[key]=[x];
else sum[key].push(x);
我正在返回文件夹对象的结构以及其中包含的 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] == undefined) {
sum[key] = [x[0]] ; // or simply leave it x;
} else {
sum[key].unshift(x[0]);
}
if(sum[key]==undefined) sum[key]=[x];
else sum[key].push(x);