gpt4 book ai didi

javascript - 如何在不删除顶层的情况下将嵌套对象移动到顶层?

转载 作者:行者123 更新时间:2023-11-30 08:33:49 25 4
gpt4 key购买 nike

假设我有这个数据:

data = [{
"_id" : "2fApaxgiPx38kpDLA",
"profile" : {
"name" : "Karina 1",
"avatar" : "avatar1.jpg",
"bio" : "my bio 1"
},
{
"_id" : "NXM6H4EWfeRAAhB7c",
"profile" : {
"name" : "Karina 2",
"avatar" : "avatar2.jpg",
"bio" : "my bio 2"
},
}];

我做了 _.map(data, "profile") 但它删除了顶级 _id:

wrongResult = [{
"name" : "Karina 1",
"avatar" : "avatar1.jpg",
"bio" : "my bio 1"
},
{
"name" : "Karina 2",
"avatar" : "avatar2.jpg",
"bio" : "my bio 2"
}];

如何在不移除顶层的情况下将嵌套对象移动到顶层:

expectedResult = [{
"_id" : "2fApaxgiPx38kpDLA",
"name" : "Karina 1",
"avatar" : "avatar1.jpg",
"bio" : "my bio 1"
},
{
"_id" : "NXM6H4EWfeRAAhB7c",
"name" : "Karina 2",
"avatar" : "avatar2.jpg",
"bio" : "my bio 2"
}];

非常感谢......

最佳答案

像这样的? (未测试)

_.map(data,function(d){
d.profile._id = d._id;
return d.profile;
});

关于javascript - 如何在不删除顶层的情况下将嵌套对象移动到顶层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com