Javascript 如何将嵌套的json关系值对象与lodash链接?

Javascript 如何将嵌套的json关系值对象与lodash链接?,javascript,json,lodash,Javascript,Json,Lodash,我正在尝试将/合并(真的不知道是哪个lodash函数)分配给嵌套的json对象 我有以下json结构: { "sports": [{ "id": "c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d", "name": "Soccer", "slug": "soccer" }], "competitions": [{ "id": "4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe

我正在尝试将/合并(真的不知道是哪个lodash函数)分配给嵌套的json对象

我有以下json结构:

{
"sports": [{
        "id": "c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d",
        "name": "Soccer",
        "slug": "soccer"
    }],
"competitions": [{
        "id": "4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe",
        "name": "English Premier League",
        "sportId": "c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d"
    }],
"contests": [{
        "id": "09cee598-7736-4941-b5f5-b26c9da113fc",
        "name": "Super Domingo Ingles",
        "status": "live",
        "competitionId": "4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe"
    }]
}
我想得到一个
contest
对象,其关系链接嵌套。预期对象如下所示:

{
        "id": "09cee598-7736-4941-b5f5-b26c9da113fc",
        "name": "Super Domingo Ingles",
        "status": "live",
        "competition": {
           "id": "4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe",
           "name": "English Premier League",
           "sport": {
             "id": "c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d",
              "name": "Soccer",
              "slug": "soccer"
            }
        }
    }]
}

我如何使用lodash来完成这种关系?它也可以使用纯javascript。

您不需要任何特殊的赋值运算符或lodash。您只需使用
=

ogObject={
“体育”:[{
“id”:“c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d”,
“名称”:“足球”,
“鼻涕虫”:“足球”
}],
“竞赛”:[{
“id”:“4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe”,
“名称”:“英超联赛”,
“sportId”:“c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d”
}],
“竞赛”:[{
“id”:“09cee598-7736-4941-b5f5-b26c9da113fc”,
“名称”:“超级多明戈英格尔”,
“状态”:“现场”,
“竞争ID”:“4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe”
}]
};
newObject=ogObject.competings[0];

对于(var i=0;i,没有内置的lodash函数可用于展平关系JSON结构

const sourceJSON={
“体育”:[{
“id”:“c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d”,
“名称”:“足球”,
“鼻涕虫”:“足球”
}],
“竞赛”:[{
“id”:“4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe”,
“名称”:“英超联赛”,
“sportId”:“c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d”
}],
“竞赛”:[{
“id”:“09cee598-7736-4941-b5f5-b26c9da113fc”,
“名称”:“超级多明戈英格尔”,
“状态”:“现场”,
“竞争ID”:“4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe”
}]
}
函数findSport(源,sportId){
让sport=u3;.find(source['sports'],{id:sportId});
如果(!运动){
返回{};
}
返回{
id:sport.id,
名称:sport.name,
鼻涕虫:运动,鼻涕虫,
}	
}
功能查找竞争(来源,竞争ID){
让competition=uu.find(source['competitions',{id:competitionId});
如果(!竞争){
返回{};
}
返回{
id:competition.id,
名称:competition.name,
sport:findSport(来源,competition.sportId),
}
}
功能竞赛(来源){
return.map(源['contests'],(contest)=>{
返回{
id:contest.id,
名称:contest.name,
状态:竞赛,状态,
竞争:findCompetition(来源、竞争、竞争ID),
}
});	
}
console.log(竞争(sourceJSON));

您确实需要向我们展示您所做的尝试,以便我们可以就您面临的问题向您提供建议,否则您只需要代码编写服务($)

然而,在ES2016中,您可以这样做

“严格使用”;
常量对象={
体育:[{
id:'c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d',
名称:"足球",,
鼻涕虫:“足球”,
}],
比赛:[{
id:'4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe',
名称:“英超联赛”,
sportId:'c60d0c48-151e-4fa2-bdf8-48cdfa77ad1d',
}],
比赛:[{
id:'09cee598-7736-4941-b5f5-b26c9da113fc',
名称:“超级多明戈英格尔”,
状态:“live”,
竞争ID:'4c19ca7c-4d17-46ce-bb4e-e25a4ebe5dbe',
}],
};
const transformed=对象竞赛地图((竞赛)=>{
const competition=obj.competitions.find(item=>item.id==contest.competitionId);
const sport=obj.sports.find(item=>item.id==competition.sportId);
const sportLevel={…sport};
const competitionLevel={……竞争,运动:sportLevel};
删除competitionLevel.sportId;
常量竞赛级别={…竞赛,竞赛:竞赛级别};
删除竞争级别.competitionId;
回报水平;
});

console.log(JSON.stringify(transformed,null,2));
您尝试了什么,出现了什么问题?