如何将这个嵌套的对象数组转换为单个对象数组?

时间:2018-01-31 15:48:38

标签: 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: "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

我想将第一个提取的集合与第二个提取的集合相结合。我该怎么做?

7 个答案:

答案 0 :(得分:3)

您可以使用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; }

答案 1 :(得分:2)

我觉得一个简单的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"
}]

答案 2 :(得分:2)

您可以使用对象中的某些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)

答案 3 :(得分:1)

我只是遍历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);

答案 4 :(得分:1)

您可以将array#maparray#reduceObject.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);

答案 5 :(得分:1)

这非常简单:

profilesData.map(data => Object.assign({}, data.profile, { photos: data.photo_details.photos }));

或者,如果您只定位到最近的环境,或者您正在使用例如Babel,您可以使用parameter destructuringobject 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%}

答案 6 :(得分:0)

嗯,似乎已经迟到但我认为这会奏效 ```

ng new <project-name>

```