使用破坏和对象格式化。

发布于 2025-02-04 10:56:49 字数 1445 浏览 2 评论 0原文

我正在启动一个新的React项目,我的API返回了我的JSON,其中这种格式不是我想要的会议。我尝试使用破坏性和object.entries。我拥有的:

 {
        "meetings": [
            {
                "room1": DC13,
                "room2": AP47,
            }
        ],
        "teachers": [
            {
                "name: "Patrick",
                "speciality":"Math"
            },
           ...
        ],
        "alumni": [
            {
                "firstName": "Joe",
                "lastName":"Luis"
            },
            ...
        ]
      
    }

我想要的:

   {
            "meetings": [
                {
                    "name": room1,
                    "location": DC13,
                },
                {
                    "name": room2,
                    "location": AP47,
                },

            ],
            "teachers": [
                {
                    "name: "Patrick",
                    "speciality":"Math"
                },
               ...
            ],
            "alumni": [
                {
                    "firstName": "Joe",
                    "lastName":"Luis"
                },
                ...
            ]
          
        }

我尝试过但不起作用的是:

const data= {meetings, teachers, alumni}
const newData = Object.entries(meetings).reduce((acc,name,location])=>acc.concat({name,location}),[]))

然后使用此newdata进行操作。

I'm starting a new project in React and my API returns me a JSON where the format is not what I want for meetings. I've tried to use destructuring and Object.entries. What I have:

 {
        "meetings": [
            {
                "room1": DC13,
                "room2": AP47,
            }
        ],
        "teachers": [
            {
                "name: "Patrick",
                "speciality":"Math"
            },
           ...
        ],
        "alumni": [
            {
                "firstName": "Joe",
                "lastName":"Luis"
            },
            ...
        ]
      
    }

What I want:

   {
            "meetings": [
                {
                    "name": room1,
                    "location": DC13,
                },
                {
                    "name": room2,
                    "location": AP47,
                },

            ],
            "teachers": [
                {
                    "name: "Patrick",
                    "speciality":"Math"
                },
               ...
            ],
            "alumni": [
                {
                    "firstName": "Joe",
                    "lastName":"Luis"
                },
                ...
            ]
          
        }

What I've tried but not working:

const data= {meetings, teachers, alumni}
const newData = Object.entries(meetings).reduce((acc,name,location])=>acc.concat({name,location}),[]))

and then use this newData for manipulation after.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2025-02-11 10:56:49

看来,使用MAP此处比redable更干净,因此,这是使用MAP的工作解决方案。

const data = { meetings, teachers, alumni };
data.meetings = Object.entries(data.meetings[0]).map(([key, value]) => ({
  "name": key,
  "location": value,
}))

It seems that using map here would be cleaner than reduce, so here's a working solution using map.

const data = { meetings, teachers, alumni };
data.meetings = Object.entries(data.meetings[0]).map(([key, value]) => ({
  "name": key,
  "location": value,
}))
戏舞 2025-02-11 10:56:49

这为您带来了所需的输出。它通过将其输入映射到新的对象中,用name位置键将对象映射并重组对象。

const data = {
    meetings: [
        {
            room1: "DC13",
            room2: "AP47",
        }
    ],
    teachers: [
        {
            name: "Patrick",
            speciality: "Math",
        },
    ],
    alumni: [
        {
            firstName: "Joe",
            lastName: "Luis",
        },
    ],
}

const newData = {
  ...data,
  meetings: data.meetings.map(meeting =>
    Object.entries(meeting)
      .map(([name, location]) =>
        ({name, location})
      )
    )
}

console.log(newData);

This gets you the desired output. It maps it each meeting and restructures the object by mapping its entry into a new object with the name and location keys.

const data = {
    meetings: [
        {
            room1: "DC13",
            room2: "AP47",
        }
    ],
    teachers: [
        {
            name: "Patrick",
            speciality: "Math",
        },
    ],
    alumni: [
        {
            firstName: "Joe",
            lastName: "Luis",
        },
    ],
}

const newData = {
  ...data,
  meetings: data.meetings.map(meeting =>
    Object.entries(meeting)
      .map(([name, location]) =>
        ({name, location})
      )
    )
}

console.log(newData);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文