2017-08-06 94 views
2

我想分配/合并(真的不知道哪个lodash函数)嵌套json对象。如何链接嵌套的json关系值对象与lodash?

我有以下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。

+2

你尝试过什么,什么是问题? – Xotic750

回答

0

你不需要任何特殊的赋值操作符或lodash。您只需使用=

ogObject = { 
 
"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" 
 
    }] 
 
}; 
 
newObject = ogObject.contests[0]; 
 
for(var i = 0; i<ogObject.competitions.length;i++){ 
 
    if(ogObject.competitions[i].id == newObject.competitionId){ 
 
     newObject.competition = ogObject.competitions[i]; 
 
     for(var j = 0; j<ogObject.sports.length;j++){ 
 
      if(ogObject.sports[j].id == newObject.competition.sportId){ 
 
       newObject.competition.sport = ogObject.sports[j]; 
 
       break; 
 
      } 
 
     } 
 
     break; 
 
    } 
 
} 
 
console.log(newObject)

这可能是从lodash一个内置的,但我对此表示怀疑。这需要预先了解您的模式与sportId和sports,competitionId和比赛等之间的关系。

0

没有内置的lodash函数可以用来扁平化关系JSON结构。但是,这样的事情应该为你工作:

const sourceJSON = { 
 
    "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" 
 
     }] 
 
    } 
 

 
    function findSport(source, sportId) { 
 
    \t let sport = _.find(source['sports'], {id: sportId}); 
 
    \t if(!sport) { 
 
    \t \t return {}; 
 
    \t } 
 
    
 
    \t return { 
 
    \t \t id: sport.id, 
 
    \t \t name: sport.name, 
 
    \t \t slug: sport.slug, 
 
    \t } \t 
 
    } 
 
    
 
    function findCompetition(source, competitionId) { 
 
    \t let competition = _.find(source['competitions'], {id: competitionId}); 
 
    \t if(!competition) { 
 
    \t \t return {}; 
 
    \t } 
 
    
 
    \t return { 
 
    \t \t id: competition.id, 
 
    \t \t name: competition.name, 
 
    \t \t sport: findSport(source, competition.sportId), 
 
    \t } 
 
    } 
 
    
 
    function flattenContests(source) { 
 
    \t return _.map(source['contests'], (contest) => { 
 
    \t \t return { 
 
    \t \t \t id: contest.id, 
 
    \t \t \t name: contest.name, 
 
    \t \t \t status: contest.status, 
 
    \t \t \t competition: findCompetition(source, contest.competitionId), 
 
    \t \t } 
 
    \t }); \t 
 
    } 
 
    
 
    console.log(flattenContests(sourceJSON));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

注意,考虑到你原来的JSON,扁平的对象应该可能是contests数组(因为contests本身是一个数组),而不是你期待的单一比赛对象。

0

你真的需要告诉我们你已经尝试了什么,以便我们可以告诉你有关你面临的问题,否则你只是要求一个代码编写服务($)。

但是,在ES2016中,您可以做到这一点。

'use strict'; 
 

 
const obj = { 
 
    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', 
 
    }], 
 
}; 
 

 
const transformed = obj.contests.map((contest) => { 
 
    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 = { ...competition, sport: sportLevel }; 
 
    delete competitionLevel.sportId; 
 
    const contestLevel = { ...contest, competition: competitionLevel }; 
 
    delete contestLevel.competitionId; 
 
    return contestLevel; 
 
}); 
 

 
console.log(JSON.stringify(transformed, null, 2));