0

How can I get the 'address' array within of request response object below?

{
    "title": "MRS.",
    "name": "Aluno",
    "lastName": "3",
    "birthday": "2019-08-31",
    "address": [
        {
            "addressName": "rua 2",
            "zipCode": "13901264",
            "city": "amparo",
            "state": "sp",
            "country": "brasil"
        },
        {
            "addressName": "rua 2",
            "zipCode": "13901264",
            "city": "amparo",
            "state": "sp",
            "country": "brasil"
        },
    ]
}

If I save this object in a state called customer and print console.log(customer.address) it works well and I can see the address informations, but I can't use map or forEach methods like customer.address.forEach, I receive an error :(

9
  • 2
    the property address is an object not an array. Which is why array methods such as forEach won't work on it, Object.values(customer.address) will give you an array of values which you can use forEach on. Similarly Object.keys to get an array of the keys, and Object.entries to get an array of [key, value] pairs Commented Nov 16, 2020 at 2:00
  • I asked wrong, i wanna get the address object instead address array haha Commented Nov 16, 2020 at 2:23
  • Sometimes iterating a well defined object is a code smell. You could use the properties directly console.log(customer.address.addressName, customer.address.city) etc Commented Nov 16, 2020 at 2:24
  • Right, look at the question. I fixed it. I wanna get the address array, sorry!! Commented Nov 16, 2020 at 2:34
  • What error are you getting? Can you please show more of your code, with the code you have now it should be working. You're most likely trying to access address before customer has been populated with data Commented Nov 16, 2020 at 2:42

3 Answers 3

2

You can use methods such as map, reduce, filter, forEach only on Array Not on Object.

the key address has value as an object, To read it you can simply use

console.log(customer.address.addressName) //street x
console.log(customer.address.zipCode) //13901264

If you want to loop through properties


Object.values(customer.address).forEach((value) => {
  console.log(value);
})

// OR

Object.keys(customer.address).forEach((key) => {
  console.log(customer.address[key]);
})

Sign up to request clarification or add additional context in comments.

3 Comments

Can you update the question with the expected response / output ?
I asked wrong, I wanna get the address object instead address array haha
Right, look at the question. I fixed it. I wanna get the address array, sorry!!
1

The property address it not an array, if you need to convert it into an array of one element you can use:

const valueAsArray = [response.address]

Comments

1
Object.keys(out.address).map(key => {
    console.log(out.address[key]);
    // etc ...
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.