如何将这个嵌套的对象数组转换为单个对象数组?
How do I transform this nested array of objects into a single array of objects?
我的 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上面的代码
我想采摘并合并第一个提取的集合和第二个提取的集合。我该怎么做?
我觉得一个简单的 map
迭代就可以满足您的需求。
const result = profilesData.map(data => {
return {
id: data.profile.id,
some: data.profile.some,
other: data.profile.other,
photos: data.photo_details.photos
}
})
console.log(result);
//result
[{
id: "26144385",
other: "misc",
photos: {
medium: "baz-1",
small: "bar-1"
}],
some: "more"
}, {
id: "26144334",
other: "some more",
photos: {
medium: "baz-2",
small: "bar-2"
}, {
medium: "buzz-2",
small: "fizz-2"
}],
some: "even"
}]
我将循环遍历 profilesData 数组并创建一个新数组,该数组使用您需要的属性重建对象。有点像..
var data = [];
for(var i=0;i<profilesData.length;i++){
data[i] = profilesData[i].profile;
data[i].photos = profilesData[i].photo_details.photos
}
console.log(data);
您可以将 array#map
和 array#reduce
与 Object.assign()
一起使用
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" } ] } } ],
result = profilesData.map(o => Object.values(o).reduce((r,o) => Object.assign(r, o), {}));
console.log(result);
您可以使用 destructuring assignment 并为数组的每个元素组装一个新对象。
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" }] } }],
result = profilesData.map(
({ profile: { id, some, other }, photo_details: { photos } }) =>
({ id, some, other, photos })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
或 spread syntax ...
用于即将发布的 JS 上的对象。这适用于 BABEL。
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" }] } }],
result = profilesData.map(
({ profile, photo_details: { photos } }) =>
({ ...profile, photos })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这很简单:
profilesData.map(data => Object.assign({}, data.profile, { photos: data.photo_details.photos }));
或者,如果您只针对最新的环境,或者您正在使用例如Babel,你可以用 parameter destructuring and object spread:
让它更简洁
profilesData.map(({profile, photo_details: {photos}}) => ({ ...profile, photos }));
您可以在下面的代码片段中看到两者的实际效果。
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" }
]
}
}
];
const profilesWithPhotos = profilesData.map(data =>
Object.assign({}, data.profile, { photos: data.photo_details.photos }));
console.log(profilesWithPhotos);
console.log('-----');
const profilesWithPhotos2 =
profilesData.map(({profile, photo_details: {photos}}) => ({ ...profile, photos }));
console.log(profilesWithPhotos2);
.as-console-wrapper{min-height:100%}
您可以在对象中使用一些 ES6 参数解构和扩展语法来做到这一点。
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"}]}}]
const result = profilesData.map(({profile, photo_details: {photos}}) => {
return {...profile, photos}
})
console.log(result)
好吧,似乎晚了,但我认为这会奏效
```
const transformedProfilesData = profilesData.reduce((array, object) => {
const profile = {...object.profile}
const photos = object.photo_details.photos.map(photo => photo)
profile.photos = photos
array.push(profile)
return array
}, [])
```
我的 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上面的代码
我想采摘并合并第一个提取的集合和第二个提取的集合。我该怎么做?
我觉得一个简单的 map
迭代就可以满足您的需求。
const result = profilesData.map(data => {
return {
id: data.profile.id,
some: data.profile.some,
other: data.profile.other,
photos: data.photo_details.photos
}
})
console.log(result);
//result
[{
id: "26144385",
other: "misc",
photos: {
medium: "baz-1",
small: "bar-1"
}],
some: "more"
}, {
id: "26144334",
other: "some more",
photos: {
medium: "baz-2",
small: "bar-2"
}, {
medium: "buzz-2",
small: "fizz-2"
}],
some: "even"
}]
我将循环遍历 profilesData 数组并创建一个新数组,该数组使用您需要的属性重建对象。有点像..
var data = [];
for(var i=0;i<profilesData.length;i++){
data[i] = profilesData[i].profile;
data[i].photos = profilesData[i].photo_details.photos
}
console.log(data);
您可以将 array#map
和 array#reduce
与 Object.assign()
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" } ] } } ],
result = profilesData.map(o => Object.values(o).reduce((r,o) => Object.assign(r, o), {}));
console.log(result);
您可以使用 destructuring assignment 并为数组的每个元素组装一个新对象。
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" }] } }],
result = profilesData.map(
({ profile: { id, some, other }, photo_details: { photos } }) =>
({ id, some, other, photos })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
或 spread syntax ...
用于即将发布的 JS 上的对象。这适用于 BABEL。
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" }] } }],
result = profilesData.map(
({ profile, photo_details: { photos } }) =>
({ ...profile, photos })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这很简单:
profilesData.map(data => Object.assign({}, data.profile, { photos: data.photo_details.photos }));
或者,如果您只针对最新的环境,或者您正在使用例如Babel,你可以用 parameter destructuring and object spread:
让它更简洁profilesData.map(({profile, photo_details: {photos}}) => ({ ...profile, photos }));
您可以在下面的代码片段中看到两者的实际效果。
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" }
]
}
}
];
const profilesWithPhotos = profilesData.map(data =>
Object.assign({}, data.profile, { photos: data.photo_details.photos }));
console.log(profilesWithPhotos);
console.log('-----');
const profilesWithPhotos2 =
profilesData.map(({profile, photo_details: {photos}}) => ({ ...profile, photos }));
console.log(profilesWithPhotos2);
.as-console-wrapper{min-height:100%}
您可以在对象中使用一些 ES6 参数解构和扩展语法来做到这一点。
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"}]}}]
const result = profilesData.map(({profile, photo_details: {photos}}) => {
return {...profile, photos}
})
console.log(result)
好吧,似乎晚了,但我认为这会奏效 ```
const transformedProfilesData = profilesData.reduce((array, object) => {
const profile = {...object.profile}
const photos = object.photo_details.photos.map(photo => photo)
profile.photos = photos
array.push(profile)
return array
}, [])
```