kplus
kplus

Reputation: 832

how to extract only the array of phone numbers from a contact with ngcordova plugin inside ionic app

I am trying to extract only the array of comma separated phone numbers without the display name and other details like emails from contact with ngcordova contact plugin.

I am using http://underscorejs.org/ to filter the result, so I could get only the contacts with at least one phone number.
my code looks like this:

 $scope.getPhoneContactList = function() 
 {
 $ionicLoading.show({
              template: 'Loading Phone numbers...'
                   });
 // options 
var options = {};
options.multiple = true;
options.hasPhoneNumber = true;
options.fields = ['phoneNumbers'];

$cordovaContacts.find(options).then(
   function(result) 
    {
    $scope.contacts = result;

    var contactsWithAtLeastOnePhoneNumber = _.filter(result,function(contact){
        return contact.phoneNumbers.length > 0
    });        

     $ionicLoading.hide();

    // display phone number 
    var alertPopup = $ionicPopup.alert
            ({
                 title: 'Phone contacts!!',
                   template:  contactsWithAtLeastOnePhoneNumber
            });      

   }, 
function(error) 
   {
   var alertPopup = $ionicPopup.alert
            ({
                 title: 'Error!!',
                   template:  error
            });      
  });
}

below is the sample of a typical contact

        "displayName": "Gajotres",
        "name": {
            "givenName"  : "Dragan",
            "familyName" : "Gaic",
            "formatted"  : "Dragan Gaic"
        },
        "nickname": 'Gajotres',
        "phoneNumbers": [
            {
                "value": "+385959052082",
                "type": "mobile"
            },
            {
                "value": "+385914600731",
                "type": "phone"
            }               
        ],
        "emails": [
            {
                "value": "[email protected]",
                "type": "home"
            }
        ],
        "addresses": [
            {
                "type": "home",
                "formatted": "Some Address",
                "streetAddress": "Some Address",
                "locality":"Zagreb",
                "region":"Zagreb",
                "postalCode":"10000",
                "country":"Croatia"
            }
        ],
        "ims": null,
        "organizations": [
            {
                "type": "Company",
                "name": "Generali",
                "department": "IT",
                "title":"Senior Java Developer"
            }
        ],
        "birthday": Date("08/01/1980"),
        "note": "",
        "photos": [
            {
                "value": "https://pbs.twimg.com/profile_images/570169987914924032/pRisI2wr_400x400.jpeg"
            }
        ],
        "categories": null,
        "urls": null

My desired result is something like this

["385959052082","+385914600731","+586914600731","+285914600731",.........]

Upvotes: 1

Views: 916

Answers (3)

omarjmh
omarjmh

Reputation: 13888

Working JsFiddle:

https://jsfiddle.net/90x3aquh/8/

You can use _.pluck to grab deeply nested properties from objects, it requires the underscore library, which you are already using! :

*Underscore _.pluck Docs:** http://underscorejs.org/#pluck

var contact = [
  {
  "phoneNumbers": [
            {
                "value": "+385959052082",
                "type": "mobile"
            },
            {
                "value": "+385914600731",
                "type": "phone"
            }               
        ]
  }
];

var nums = _.pluck(contact, 'phoneNumbers');

console.log(JSON.stringify(nums));
// logs: [[{"value":"+385959052082","type":"mobile"},{"value":"+385914600731","type":"phone"}]]

From the console of the JsFiddle:

enter image description here

Upvotes: 1

HARITHA UPPARA
HARITHA UPPARA

Reputation: 274

I have tried to get all contact numbers by using for loop... Hope it may help...

$scope.getContacts = function() {
      $scope.phoneContacts = [];

      function onSuccess(contacts) {
        for (var i = 0; i < contacts.length; i++) {
          var contact = contacts[i].phoneNumbers;
          if (contact != null){
            for(j=0; j< contact.length; j++){
              $scope.phoneContacts.push(contact[j].value);
            }
          }
        }
        console.log($scope.phoneContacts);
      };

      function onError(contactError) {
        alert(contactError);
      };

      var options = {};
      options.multiple = true;
      $cordovaContacts.find(options).then(onSuccess, onError);

  }

Upvotes: 0

rabruce
rabruce

Reputation: 133

_.pluck should be what you are looking for. It extracts one property from each item in the array.

_.pluck(contactsWithAtLeastOnePhoneNumber, 'phoneNumbers')

This should return an array of phoneNumber arrays.

Fiddle for example: https://jsfiddle.net/72komfra/

Upvotes: 1

Related Questions