Javascript 如何将这个嵌套的对象数组转换为单个对象数组?
我的api返回了以下配置文件对象数组:Javascript 如何将这个嵌套的对象数组转换为单个对象数组?,javascript,arrays,functional-programming,data-manipulation,purely-functional,Javascript,Arrays,Functional Programming,Data Manipulation,Purely Functional,我的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: "
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] ] } ]
…这不是我想要的
下面是一个包含上述代码的示例
我想采集并合并第一个提取的集合和第二个提取的集合。我如何做到这一点?我觉得一个简单的
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"
}]
我觉得一个简单的
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);
var数据=[];
对于(var i=0;i我只需循环遍历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);
var数据=[];
对于(var i=0;i您可以使用array#map
和array#reduce
和Object.assign()
const profilesData=[{profile:{id:{26144385],一些:“更多”,其他:“杂项”},照片细节:{photos:{small:“bar-1”,medium:{baz-1}}},{profile:{id:{26144334],一些:“偶数”,其他:“更多”},照片细节:{photos:{photos:[{small:“bar-2”,medium:{small:“fizz-2”,medium:“buzz 2”},
result=profilesData.map(o=>Object.values(o).reduce((r,o)=>Object.assign(r,o),{}));
console.log(result);
您可以使用array#map
和array#reduce
和Object.assign()
const profilesData=[{profile:{id:{26144385],一些:“更多”,其他:“杂项”},照片细节:{photos:{small:“bar-1”,medium:{baz-1}}},{profile:{id:{26144334],一些:“偶数”,其他:“更多”},照片细节:{photos:{photos:[{small:“bar-2”,medium:{small:“fizz-2”,medium:“buzz 2”},
result=profilesData.map(o=>Object.values(o).reduce((r,o)=>Object.assign(r,o),{}));
console.log(result);
您可以为数组的每个元素使用一个和一个新对象
const
简介数据=[{profile:{id:{26144385],一些:“更多”,其他:“杂项”,照片细节:{photos:[{small:“bar-1”,medium:“baz-1”}}},{profile:{id:“26144334”,一些:“偶数”,其他:“一些更多”},照片细节:{photos:[{small:“bar-2”,medium:“baz-2”},{small:“fizz 2”},},
结果=profilesData.map(
({profile:{id,some,other},photo_details:{photos}})=>
({id,一些,其他,照片})
);
console.log(结果);
.as console wrapper{max height:100%!important;top:0;}
您可以为数组的每个元素使用一个和一个新对象
const
简介数据=[{profile:{id:{26144385],一些:“更多”,其他:“杂项”,照片细节:{photos:[{small:“bar-1”,medium:“baz-1”}}},{profile:{id:“26144334”,一些:“偶数”,其他:“一些更多”},照片细节:{photos:[{small:“bar-2”,medium:“baz-2”},{small:“fizz 2”},},
结果=profilesData.map(
({profile:{id,some,other},photo_details:{photos}})=>
({id,一些,其他,照片})
);
console.log(结果);
.as控制台包装{max height:100%!important;top:0;}
这非常简单:
profilesData.map(data => Object.assign({}, data.profile, { photos: data.photo_details.photos }));
或者,如果您只针对最新的环境,或者您正在使用Babel等工具进行传输,您可以使用和使其更加简洁:
在下面的代码段中,您可以看到这两种方法都在起作用
const profilesData=[
{
简介:{id:“26144385”,一些:“更多”,其他:“杂项”},
照片(详情){
照片:[{小型:“bar-1”,中型:“baz-1”}]
}
},
{
简介:{id:“26144334”,有些“甚至”,有些“更多”},
照片(详情){
照片:[
{小:“bar-2”,中:“baz-2”},
{小号:“嘶嘶-2”,中号:“嘶嘶-2”}
]
}
}
];
const profilesWithPhotos=profilesData.map(数据=>
赋值({},data.profile,{photos:data.photo_details.photos});
console.log(profilesWithPhotos);
console.log('----');
常量配置文件WITHPHOTOS2=
profilesData.map({profile,photo_details:{photos}})=>({…profile,photos}));
console.log(profilesWithPhotos2);
。作为控制台包装{min height:100%}
这非常简单:
profilesData.map(data => Object.assign({}, data.profile, { photos: data.photo_details.photos }));
或者,如果您只针对最新的环境,或者您正在使用Babel等工具进行传输,您可以使用和使其更加简洁:
在下面的代码段中,您可以看到这两种方法都在起作用
const profilesData=[
{
简介:{id:“26144385”,一些:“更多”,其他:“杂项”},
照片(详情){
照片:[{小型:“bar-1”,中型:“baz-1”}]
}
},
{
简介:{id:“26144334”,有些“甚至”,有些“更多”},
照片(详情){
照片:[
{小:“bar-2”,中:“baz-2”},
{小号:“嘶嘶-2”,中号:“嘶嘶-2”}
]
}
}
];
const profilesWithPhotos=profilesData.map(数据=>
赋值({},data.profile,{photos:data.photo_details.photos});
console.log(profilesWithPhotos);
console.log('----');
常量配置文件WITHPHOTOS2=
profilesData.map({profile,photo_details:{photos}})=>({…profile,photos}));
console.log(profilesWithPhotos2);
。作为控制台包装器{min height:100%}
您可以通过一些ES6参数的解构和对象中的扩展语法来实现这一点
const profilesData=[{“profile”:{“id”:“26144385”,“some”:“more”,“other”:“misc”},“photo\u details”:{“photos”:[{“small”:“bar-1”,“medium”:“baz-1”}}}},{“profile”:{“id”:“26144334”,“some”:“偶”,“other”:“some more”},“photo\u details”:{“photos”: