重映射一个数组,在JavaScript中使每个条目上方有一个对象。

huangapple go评论64阅读模式
英文:

Remap an array to have an object above each entry in javascript

问题

以下是翻译好的部分:

我有一个Prisma查询,它返回一个名为CommunityMember的数据透视表上的用户。这个数据透视表关联到User表。因此,我的对User表的查询结果不会直接放在回复的顶部。我有很多函数设计为在用户对象的基础上运行,所以我正在尝试找出如何将用户映射到我的查询结果中,以便这些函数可以正确运行。我尝试了很多映射的组合,但没有成功。你们有什么想法吗?

这是我的Prisma查询:

members = await prisma.user.findMany({
select: {
id: true,
username: true,
name: true,
bio: true,
avatar: true,
},
skip: 10 * Number(page),
take: 10,
});


它返回结果如下:

members:
[{id: 2, username: 'man}, {id: 3, username: 'dan'}]


我希望它看起来像这样:members:

members:
[{user: {id: 2, username: 'man'}}, {user: {id: 3. username: 'dan'}}]


如果我运行`members[0].user`,我应该能够获得数据。这似乎是一个简单的映射函数,但我一直无法让它工作。

另一个示例,展示了我得到的结果,但不想要的结果。

members: [
{
id: 2,
username: 'man',
name: 'A Man',
bio: 'A man.',
avatar: [Object]
},
{
id: 3,
username: 'dude',
name: 'Dude',
bio: "Dude is a #1 developer.",
avatar: [Object]
},


这是我想要的结果。

members: [
{
// 注意每个条目上方的用户对象
user: {
id: 2,
username: 'man',
name: 'A Man',
bio: 'Man.',
avatar: [Object]
}
},
{
user: {
id: 3,
username: 'dude',
name: 'Dude',
bio: "Dude is a #1 developer.",
avatar: [Object]
}
},
]

英文:

I have a prisma query that returns users on a pivot table CommunityMember. The pivot table associates to the table User. As a result, my queries into the User table do not place the object user on top of the replies. I have a lot of functions designed to run with the user object on top so I am trying to figure out how to map user into my returns so the functions can run correctly. I've tried a lot of combinations of map and have had no luck. Do you all have any ideas?

Here is my prisma query:

        members = await prisma.user.findMany({
            select: {
                id: true,
                username: true,
                name: true,
                bio: true,
                avatar: true,
            },
            skip: 10 * Number(page),
            take: 10,
        });

It gives a result like

members:
 [{id: 2, username: 'man}, {id: 3, username: 'dan'}]

I want it to look like: members:

members:
[{user: {id: 2, username: 'man'}}, {user: {id: 3. username: 'dan'}}]

If I run members[0].user I should get the data inside. It seems like a simple map function, but I have not been able to get it to work.

Another example of what I get, but do not want.

members:  [
  {
      id: 2,
      username: 'man',
      name: 'A Man',
      bio: 'A man.',
      avatar: [Object]
  },
  {
      id: 3,
      username: 'dude',
      name: 'Dude',
      bio: "Dude is a #1 developer.",
      avatar: [Object]
  },

This is what I want.

members:  [
  {
    //Notice the user object on top of each entry
    user: {
      id: 2,
      username: 'man',
      name: 'A Man',
      bio: 'Man.',
      avatar: [Object]
    }
  },
  {
    user: {
      id: 3,
      username: 'dude',
      name: 'Dude',
      bio: "Dudes a #1 developer.",
      avatar: [Object]
    }
  },
]

答案1

得分: 1

不确定我完全理解整个情况,但这个是否可以?

    const members = [
      {
          id: 2,
          username: 'man',
          name: 'A Man',
          bio: 'A man.',
          avatar: {}
      },
      {
          id: 3,
          username: 'dude',
          name: 'Dude',
          bio: "Dude is a #1 developer.",
          avatar: {}
      }
    ].map(member => ({user: member}))
英文:

Not sure I got the whole picture but wouldn't this do?

    const members = [
      {
          id: 2,
          username: 'man',
          name: 'A Man',
          bio: 'A man.',
          avatar: {}
      },
      {
          id: 3,
          username: 'dude',
          name: 'Dude',
          bio: "Dude is a #1 developer.",
          avatar: {}
      }
    ].map(member => ({user: member}))

答案2

得分: 0

if your object looks like:
obj = {members: [{...}, ..]}

then an Array.map will solve the problem:

const userMembers = obj.members.map((member) => ({user: member});
obj.members = userMembers;

英文:

if your object looks like:
obj = {members: [{...}, ..]}

then an Array.map will solve the problem:

const userMembers = obj.members.map((member) => ({user: member});
obj.members = userMembers;

huangapple
  • 本文由 发表于 2023年2月19日 23:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定