How to show a specific array that is in a JSON?

Asked

Viewed 45 times

-2

{
"id": "1",
"addresses": [
  {
    "num": "1"
    "addressType": "Comercial",
    "country": "Brasil",
    "postalCode": "89035200",
  },
  {
    "num": "3"
    "addressType": "Comercial",
    "country": "Brasil",
    "postalCode": "89035200",
  },
  {
    "num": "3"
    "addressType": "Comercial",
    "country": "Brasil",
    "postalCode": "89035200",
  }
],

How could I return all the "num" belonging to "Adress" within an array?

1 answer

1


const input = {
"id": "1",
"addresses": [
{
  "num": "1",
  "addressType": "Comercial",
  "country": "Brasil",
  "postalCode": "89035200",
},
{
  "num": "3",
  "addressType": "Comercial",
  "country": "Brasil",
  "postalCode": "89035200",
},
{
  "num": "3",
  "addressType": "Comercial",
  "country": "Brasil",
  "postalCode": "89035200",
}
]};
  
const output1 = input.addresses.map(a => a.num);
console.log(output1);
const output2 = input.addresses.map(a => ({"num": a.num}));
console.log(output2);

How about something like this, like in output1 or output2?

Browser other questions tagged

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