Javascript 如何获取用户阵列只尝试了一切

Javascript 如何获取用户阵列只尝试了一切,javascript,reactjs,http,native,Javascript,Reactjs,Http,Native,这是我从node.js获取的json对象,我想获取用户数组来设置我的状态。。我把所有的东西都做了三倍,但我会得到阵列的poyo宽度 {count: 2, users: Array(2)} count: 2 users: Array(2) 0: {_id: "5cdd1c260d0b3d0660d85bfc", name: "Punith", email: "puntihdd@gmail.com", request: {…}} 1: {_id: "5cdd1c4d0d0b3d0660d85bfd"

这是我从node.js获取的json对象,我想获取用户数组来设置我的状态。。我把所有的东西都做了三倍,但我会得到阵列的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: ,
    }
]

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

const yourObject = { count: 2, users: [{}, {}]};
const users = yourObject.users;
现在我们有了用户数组,我们可以重新映射它,这样我们就可以通过使用
.map()
方法只获得您想要的三个属性(\u 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={
计数:2,
用户:[{
_id:“5cdd1c260d0b3d0660d85bfc”,
名称:“普尼思”,
电子邮件:“puntihdd@gmail.com",
请求:{
a:‘价值’
}
},
{
_id:“5CDD1C4D0B3D0660D85BFD”,
姓名:“Ashwith”,
电子邮件:“ashwith@gmail.com",
请求:{
a:‘价值’
}
}
]
}
const users=myData.users;
const formattedUsers=myData.users.map(user=>({
_id:user.\u id,
name:user.name,
电子邮件:user.email
}));

console.log(格式化用户)
您可以
映射Ajax请求的响应以提取特定值。但请记住,对象内部属性的顺序并不能得到保证。如果为了返回数组而需要这些属性,那么就会丢失propertyname信息。相反,可以通过索引0,1,2访问这些值

const returnUsers = ({users}) => users.map(({_id,name,email}) => ({
     _id,name,email     
   })
或者返回一个数组

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

您可以显示代码吗?您当前如何访问它?
export-const-logIn=(电子邮件,密码)=>{console.log('loggin-in');const-res=fetch(logIn_-URL,{method:'POST',headers:{Accept:'application/json',Content-Type':'application/json',},body:json.stringify({email:email,password:password})然后(res=>res.json()).catch(err=>err);return res;}
你介意把它放在问题的主体中吗?注释不是代码示例的好地方,因为它会丢弃大多数格式。。。