【问题标题】:How do I transform this nested array of objects into a single array of objects?如何将此嵌套的对象数组转换为单个对象数组?
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案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】:

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            【解决方案5】:

            您可以将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);

            【讨论】:

              【解决方案6】:

              这很简单:

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

              或者,如果您只针对最近的环境,或者您正在转译,例如Babel,你可以用parameter destructuringobject spread 让这个更简洁:

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

              您可以在下面的 sn-p 中看到两者的作用。

              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%}

              【讨论】:

              • 注意:在其他人发布了非常相似的答案之后,我添加了第二段代码,但我在编写它时还没有阅读它们。伟大的思想等等。
              【解决方案7】:

              好吧,似乎来晚了,但我认为这会奏效 ```

              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
              }, [])
              

              ```

              【讨论】:

                猜你喜欢
                • 2020-07-03
                • 2021-05-28
                • 1970-01-01
                • 1970-01-01
                • 2019-04-26
                • 2017-03-27
                • 2017-11-15
                • 1970-01-01
                • 2020-05-03
                相关资源
                最近更新 更多