i'm trying to filter an array and get the phone numbers from an address book using lodash, the structure is something like
var obj = {
'contacts': [{
'id': 1,
'displayName': 'John Doe',
'phonenumbers': [{
'id': '1',
'type': 'mobile',
"pref":false,
'value': '555-555'
}, {
'id': '2',
'type': 'mobile',
"pref":false,
'value': '555-554'
}]
}, {
'id': 2,
'displayName': 'Jane Doe',
'phonenumbers': [{
'id': '1',
'type': 'mobile',
"pref":false,
'value': '555-557'
}]
}]
}
i can try to reach the phone numbers with something like:
lodash.map(
contacts,
function(person) {
return { id: person.id,
displayName: person.displayName,
phoneNumbers: [
{
number: person.phoneNumbers[0].value,
type: person.phoneNumbers[0].type}
],
photos: person.photos };
}
);
but when i want to get the 2 and third with person.phoneNumbers[1].value, i get errors when there is no second phone number, is there a way to get them without specifying the position in the array? the output should be a new array with the id of the contact and the phone numbers and type.
var ouptput= {
'contacts': [{
'id': 1,
'phonenumbers': [{
'type': 'mobile',
'value': '555-555'
}, {
'type': 'mobile',
'value': '555-554'
}]
}, {
'id': 2,
'phonenumbers': [{
'type': 'mobile',
'value': '555-557'
}]
}]
}