[英]How to parse only specific attributes from a JSON file to an array
I have this JSON array:我有这个 JSON 阵列:
{
"data": [
{
"id": 6,
"pnome": "dasda",
"unome": "dad",
"avatar": 1,
"email": "d",
"pass": "password",
"ponto": 0
},
{
"id": 3,
"pnome": "Enguias",
"unome": "Enguias outra vez",
"avatar": 10,
"email": "enguias@enguias.enguias",
"pass": "enguias",
"ponto": 0
},
{
"id": 2,
"pnome": "André",
"unome": "Marques",
"avatar": 1,
"email": "aglmarque@gmail.com",
"pass": "yet1",
"ponto": 0
}
]
}
And i'm putting it into an array with axios, like this:我将它放入一个带有 axios 的数组中,如下所示:
axios.get(my_url_api)
.then((res)=>{
const txt = JSON.stringify(res.data.data);
const users = JSON.parse(txt)
})
Which makes the users array have all the info in the JSON file.这使得用户数组拥有 JSON 文件中的所有信息。 How can I make it so that I only pass specific attributes like email and pass?
我怎样才能做到这一点,以便我只通过 email 等特定属性并通过? It would look like this:
它看起来像这样:
{
"data": [
{
"email": "d",
"pass": "password"
},
{
"email": "enguias@enguias.enguias",
"pass": "enguias"
},
{
"email": "aglmarque@gmail.com",
"pass": "yet1"
}
]
}
You can use Array.map()
function to remove the unnecessary fields like following example:您可以使用
Array.map()
function 删除不必要的字段,如下例所示:
let res = { "data": [ { "id": 6, "pnome": "dasda", "unome": "dad", "avatar": 1, "email": "d", "pass": "password", "ponto": 0 }, { "id": 3, "pnome": "Enguias", "unome": "Enguias outra vez", "avatar": 10, "email": "enguias@enguias.enguias", "pass": "enguias", "ponto": 0 }, { "id": 2, "pnome": "André", "unome": "Marques", "avatar": 1, "email": "aglmarque@gmail.com", "pass": "yet1", "ponto": 0 } ] } res.data = res.data.map(item => { return { email: item.email, pass: item.pass }; }); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.