How to Get Specific JSON Data with Angular 2x?

Asked

Viewed 834 times

0

I have an answer that I turn into JSON this way:

const resposta = JSON.stringify(response);

              console.log(resposta);

Give me the feedback on console.log this way:

[{"nome":"Ramos Janones","email":"[email protected]","cpf":"03472925698","celular":"34996320391","telefone":"","cep":"38300070","endereco":"DEZESSEIS","cidade":"Ituiutaba","estado":"Minas Gerais","bairro":"","complemento":"","usuario":"ramos","senha":"teste123","idppoker":"","nickppocker":"ramosinfo","numero":"","_id":"5bf18c512af2e61ab879d2f7","createdAt":"2018-11-18T15:59:13.735Z","updatedAt":"2018-11-26T15:41:00.806Z","__v":0,"id":"5bf18c512af2e61ab879d2f7","foto":{"_id":"5bfc140dbe257a17900442a1","name":"8.jpg","sha256":"Mt3053NTSRC81wiHOEMubEUagRAPlHOR-MtPvf2yKgQ","hash":"36cdc8491d7b4fa1817bed8126b3143e","ext":".jpg","mime":"image/jpeg","size":"58.46","url":"/uploads/36cdc8491d7b4fa1817bed8126b3143e.jpg","provider":"local","related":["5bf18c512af2e61ab879d2f7"],"createdAt":"2018-11-26T15:41:01.003Z","updatedAt":"2018-11-26T15:41:04.113Z","__v":0,"id":"5bfc140dbe257a17900442a1"},"contasJogadors":[]}]

How can I take the values I want to put in a localStorage?

I researched before posting this question, tried the various ways and it didn’t work.

1 answer

1


The map() method invokes the callback function passed by argument to each Array element and returns a new Array as a result.

You could use the map array, it loops and returns a new array with the modifications you want, including removing or adding fields as needed:

var data = [{"nome":"Ramos Janones","email":"[email protected]","cpf":"03472925698","celular":"34996320391","telefone":"","cep":"38300070","endereco":"DEZESSEIS","cidade":"Ituiutaba","estado":"Minas Gerais","bairro":"","complemento":"","usuario":"ramos","senha":"teste123","idppoker":"","nickppocker":"ramosinfo","numero":"","_id":"5bf18c512af2e61ab879d2f7","createdAt":"2018-11-18T15:59:13.735Z","updatedAt":"2018-11-26T15:41:00.806Z","__v":0,"id":"5bf18c512af2e61ab879d2f7","foto":{"_id":"5bfc140dbe257a17900442a1","name":"8.jpg","sha256":"Mt3053NTSRC81wiHOEMubEUagRAPlHOR-MtPvf2yKgQ","hash":"36cdc8491d7b4fa1817bed8126b3143e","ext":".jpg","mime":"image/jpeg","size":"58.46","url":"/uploads/36cdc8491d7b4fa1817bed8126b3143e.jpg","provider":"local","related":["5bf18c512af2e61ab879d2f7"],"createdAt":"2018-11-26T15:41:01.003Z","updatedAt":"2018-11-26T15:41:04.113Z","__v":0,"id":"5bfc140dbe257a17900442a1"},"contasJogadors":[]}]

var maped = data.map(function(item){
  
  return {nome: item.nome, email: item.email}
})
console.log(maped)

Using the code just return the fields you want and then save the new array as desired

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

  • Thanks @Edson Alves. Perfect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.