Javascript 将JSON添加到空数组问题

Javascript 将JSON添加到空数组问题,javascript,arrays,json,Javascript,Arrays,Json,我有一个顶级JSON结构,如下所示: { "video": [], "messages": [], "notifications": [] } { "video": [ [ { "_id": "5f98ab906439155cfc6f9afb",

我有一个顶级JSON结构,如下所示:

{
    "video": [],
    "messages": [],
    "notifications": []
}
{
    "video": [
        [
            {
                "_id": "5f98ab906439155cfc6f9afb",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:21:52.683Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    },
                    {
                        "username": "user2"
                    }
                ]
            },
            {
                "_id": "5f98aba0789e163e0c78908f",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:22:08.048Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    }
                ]
            }
        ]
    ],
    "messages": [],
    "notifications": []
}
我有一个数据库输出(在变量“result”中),如下所示,我想将其放入“video”数组中:

我的代码是:

let dashboardJSON = { "video": [], "messages": [], "notifications": [] };
dashboardJSON.video.push(result)
它可以工作,但我最终得到了太多的数组(我想)-它看起来如下所示:

{
    "video": [],
    "messages": [],
    "notifications": []
}
{
    "video": [
        [
            {
                "_id": "5f98ab906439155cfc6f9afb",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:21:52.683Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    },
                    {
                        "username": "user2"
                    }
                ]
            },
            {
                "_id": "5f98aba0789e163e0c78908f",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:22:08.048Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    }
                ]
            }
        ]
    ],
    "messages": [],
    "notifications": []
}
我想要
“视频”:[{…},{…}]
而我有
“视频”:[[{…},{…}]


如何解决这个问题?

只需使用Array.prototype.map()方法。Map方法使用提供的函数返回一个新数组,该数组包含结果

const-ret={
视频:[],
信息:[],
通知:[],
};
常数数据=[
{
_id:'5f98ab906439155cfc6f9afb',
状态:“未启动”,
日期:“2020-10-27T23:21:52.683Z”,
卡林维特:[
{
用户名:“user1”,
},
{
用户名:“user2”,
},
],
},
{
_id:'5F98ABA07889E163E0C78908F',
状态:“未启动”,
日期:“2020-10-27T23:22:08.048Z”,
卡林维特:[
{
用户名:“user1”,
},
],
},
];
ret.video=data.map((x)=>x);
控制台日志(ret)您可以按如下方式使用:

{
    "video": [],
    "messages": [],
    "notifications": []
}
{
    "video": [
        [
            {
                "_id": "5f98ab906439155cfc6f9afb",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:21:52.683Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    },
                    {
                        "username": "user2"
                    }
                ]
            },
            {
                "_id": "5f98aba0789e163e0c78908f",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:22:08.048Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    }
                ]
            }
        ]
    ],
    "messages": [],
    "notifications": []
}
让结果=[
{
“_id”:“5f98ab906439155cfc6f9afb”,
“状态”:“未启动”,
“日期”:“2020-10-27T23:21:52.683Z”,
“卡林维特”:[
{
“用户名”:“user1”
},
{
“用户名”:“user2”
}
]
},
{
“_id”:“5F98ABA07889E163E0C78908F”,
“状态”:“未启动”,
“日期”:“2020-10-27T23:22:08.048Z”,
“卡林维特”:[
{
“用户名”:“user1”
}
]
}
]
让dashboardJSON={“视频”:[],“消息”:[],“通知”:[]};
dashboardJSON.video.push(…结果)

log(dashboardJSON)太棒了,谢谢!“传播操作员”在干什么?@Kevin,不客气。它将
result
的项展开为单独推送,而不是对象列表。