【发布时间】:2018-07-10 19:08:27
【问题描述】:
我的 api 返回了这个配置文件对象数组:
const profilesData = [
{
profile: { id: "26144385", some: "more", other: "misc" },
photo_details: {
photos: [{ small: "bar-1", medium: "baz-1" }]
}
},
{
profile: { id: "26144334", some: "even", other: "some more" },
photo_details: {
photos: [
{ small: "bar-2", medium: "baz-2" },
{ small: "fizz-2", medium: "buzz-2" }
]
}
}
];
我需要对其进行转换,以便得到一个 profileWithPhotos 数组,如下所示:
const profileWithPhotos = [
{
id: "26144385",
some: "more",
other: "misc",
photos: [
{
small: "bar-1",
medium: "baz-1"
}
]
},
{
id: "26144334",
some: "even",
other: "some more",
photos: [
{
small: "bar-2",
medium: "baz-2"
},
{
small: "fizz-2",
medium: "buzz-2"
}
]
}
];
到目前为止,我已经尝试将解析分解为更小的函数:
const getProfiles = profilesData =>
profilesData.map(profileData => profileData.profile);
const getSmallAndMediumPic = pic => ({ small: pic.small, medium: pic.medium });
const getPhotos = profilesData =>
profilesData.map(profileData => profileData.photo_details.photos.map(getSmallAndMediumPic));
const profiles = getProfiles(profilesData);
const photos = getPhotos(profilesData);
const profileWithPhotos = [...profiles, { photos: photos }];
现在我得到了这种对象数组:
[ { id: '26144385', some: 'more', other: 'misc' },
{ id: '26144334', some: 'even', other: 'some more' },
{ photos: [ [Object], [Object] ] } ]
...这不是我想要的。
这是一个working jsbin,上面的代码
我想提取并组合第一个提取的集合与第二个提取的集合。我该怎么做?
【问题讨论】:
标签: javascript arrays functional-programming data-manipulation purely-functional