Created
September 13, 2015 07:05
-
-
Save nrm176/cd00b9615cbcdec0071d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = [ | |
{ | |
regionId: 1, | |
regionName: "Kanto", | |
prefectures: [ | |
{id: 1, name: "Tokyo", population: 10000}, | |
{id: 2, name: "Kanagawa", population: 20000}, | |
{id: 3, name: "Chiba", population: 300}, | |
{id: 4, name: "Gunma", population: 20} | |
] | |
}, | |
{ | |
regionId: 2, | |
regionName: 'Tohoku', | |
prefectures: [ | |
{id: 1, name: "Aomori", population: 1500}, | |
{id: 2, name: "Iwate", population: 200}, | |
] | |
} | |
] | |
function getPrefecturesFromRegionId(regionId){ | |
return data.reduce(function(p,c,idx,arr){ | |
if (c.regionId === regionId){ | |
return p.concat(c.prefectures) | |
} | |
return p | |
},[]).map(function(e){ | |
return {id: e.id, name: e.name} | |
}) | |
} | |
console.log(getPrefecturesFromRegionId(1)) | |
//[ { id: 1, name: 'Tokyo' }, | |
// { id: 2, name: 'Kanagawa' }, | |
// { id: 3, name: 'Chiba' }, | |
// { id: 4, name: 'Gunma' } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment