1

Using Logger.log(response.data.phone), I'm getting this in my log:

[{label=work, primary=true, value=5558675309}, {label=work, value=6108287680, primary=false}, {value=6105516373, label=work, primary=false}]

What I want is to return the two phone numbers as 5558675309, 6108287680.

I've tried Logger.log(response.data.phone.value) and that doesn't work. I've tried ...phone['value'], I've tried ...phone[0].value and this one does return the first phone number 5558675309. But I would like it to return all of the value values whenever I put in the phone key. So how would I modify the logger?

823g4n8901
  • 139
  • 1
  • 11
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Heretic Monkey Oct 25 '20 at 18:58

2 Answers2

2

response.data.phone is an array you can try looping through it:

Logger.log(response.data.phone.map(phone => phone.value).join(', '));

const response = {data: {phone : [{label:'work', primary:true, value:5558675309}, {label:'work', value:6108287680, primary:false}, {value:6105516373, label:'work', primary:false}] } }

const Logger = { log : console.log};

Logger.log(response.data.phone.map(phone => phone.value).join(', '));
Melchia
  • 22,578
  • 22
  • 103
  • 117
1

response.data is an array, response.data.phone does not exist. What you want is response.data[n].phone for an integer n. You can do this with a forEach loop.

response.data.forEach((element) => Logger.log(element.phone.value));

If for whatever reason you need support for older browsers you can use the older function syntax:

response.data.forEach(function(element){
    Logger.log(element.phone.value)
});
Ben
  • 2,200
  • 20
  • 30