I have this JSON array:
{
"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": "[email protected]",
"pass": "enguias",
"ponto": 0
},
{
"id": 2,
"pnome": "André",
"unome": "Marques",
"avatar": 1,
"email": "[email protected]",
"pass": "yet1",
"ponto": 0
}
]
}
And i'm putting it into an array with axios, like this:
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. How can I make it so that I only pass specific attributes like email and pass? It would look like this:
{
"data": [
{
"email": "d",
"pass": "password"
},
{
"email": "[email protected]",
"pass": "enguias"
},
{
"email": "[email protected]",
"pass": "yet1"
}
]
}
You can use Array.map()
function to remove the unnecessary fields like following example:
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": "[email protected]",
"pass": "enguias",
"ponto": 0
},
{
"id": 2,
"pnome": "André",
"unome": "Marques",
"avatar": 1,
"email": "[email protected]",
"pass": "yet1",
"ponto": 0
}
]
}
res.data = res.data.map(item => {
return {
email: item.email,
pass: item.pass
};
});
console.log(res);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments