如何获得用户数组只尝试了一切

时间:2019-05-16 20:16:58

标签: javascript reactjs http native

这是我从node.js获得的json对象,我想让users数组设置我的状态..我整理了所有内容,但我会得到 poyo 数组的宽度

{count: 2, users: Array(2)}
count: 2
users: Array(2)
0: {_id: "5cdd1c260d0b3d0660d85bfc", name: "Punith", email: "puntihdd@gmail.com", request: {…}}
1: {_id: "5cdd1c4d0d0b3d0660d85bfd", name: "Ashwith", email: "ashwith@gmail.com", request: {…}}
length: 2
__proto__: Array(0)
__proto__: Object

我想得到

[
    {
       _id: ,
       name: ,
       email: ,
    }
]

按此顺序

2 个答案:

答案 0 :(得分:-1)

嗯,看来您正在从noSQL数据库接收对象。 假设您的对象如下所示: {count: 2, users: Array(2)},我们可以通过以下方式获取用户属性:

const yourObject = { count: 2, users: [{}, {}]};
const users = yourObject.users;

现在我们有了users数组,我们可以重新映射它,以便使用.map()方法仅获得您想要的三个属性(_id,name,email):

//  exactly the same property names like you want:
users.map(x => ({ _id: x._id, name: x.name, email: x.email }));

//  or omitting the underscore on your new items to make it nicer:
users.map(x => ({ id: x._id, name: x.name, email: x.email }));

将这些概念捆绑在一起,这是一个可行的示例,可能会对您有所帮助:

const myData = {
    count: 2,
    users: [{
            _id: "5cdd1c260d0b3d0660d85bfc",
            name: "Punith",
            email: "puntihdd@gmail.com",
            request: {
                a: 'value'
            }
        },
        {
            _id: "5cdd1c4d0d0b3d0660d85bfd",
            name: "Ashwith",
            email: "ashwith@gmail.com",
            request: {
                a: 'value'
            }
        }
    ]
}

const users = myData.users;
const formattedUsers = myData.users.map(user => ({
    _id: user._id,
    name: user.name,
    email: user.email
}));

console.log(formattedUsers);

答案 1 :(得分:-1)

您可以s=df.b.diff().ne(0).cumsum() s1=s.groupby(s).transform('count') s2=s.groupby(s).cumcount() df['c']=((s1==s2+1)&(s1>3)).astype(int) df['d']=(df.c.mask(df.c==0)*df.b*2).bfill(limit=2).combine_first(df.c) df Out[87]: a b c d 0 16215 2 0 0.0 1 24848 4 0 0.0 2 24849 4 0 8.0 3 24850 4 0 8.0 4 24851 4 1 8.0 5 24852 6 0 0.0 6 24853 6 0 0.0 7 24854 8 0 0.0 8 24855 8 0 0.0 9 24856 8 0 16.0 10 25208 8 0 16.0 11 25932 8 1 16.0 12 28448 10 0 0.0 13 28449 10 0 0.0 14 28450 10 0 0.0 对Ajax请求的响应来提取某些值。但是请记住,不能保证对象内部属性的顺序。如果需要属性,则必须返回数组,但是会丢失属性名信息。而是可以通过索引0,1,2来访问这些值。

map

或返回一个数组

const returnUsers = ({users}) => users.map(({_id,name,email}) => ({
     _id,name,email     
   })