How to compaire two json array


#1

i have two different json response one is contact list json get from phonebook contacts and another one this my own db json reponse .how to get duplicte value from the two array and duplicate value will be push into another new array.how can i do that.


#2

If you can post the two arrays here, then it would be much appreciated and you will get the quick solution of your issue.


#3

You can try use array filtering.

let contacts = ...;
let current  =...;

let similar = current.filter((el) => contacts.indexOf(el) != -1);

#4

actually i want to check.phone contact list mobile number and my own db mobile number is same or not.if the value are same. then the duplicate value will be push into another array.

this is my contact fetch code


 constructor(public navCtrl: NavController, public navParams: NavParams,public contacts:Contacts,public platform: Platform,public http:Http) {

  	 this.platform.ready().then(() => {
       var opts = {   
          ///filter : "M",                                
          multiple: true,        
          hasPhoneNumber:true,                             
          fields:  [ 'displayName', 'name' ]
        };
        this.contacts.find([ 'displayName', 'name' ],opts).then((contacts) => {
          this.contactlist=contacts;
          alert(JSON.stringify(this.contactlist))
        }, (error) => {
          console.log(error);
        })
    })
     this.fetchallcontacts();
  }


fetchallcontacts(){
           this.http.get('http://xxxx.com/xxxxxx/xxxx/getAllContact.php')
            .map(res => res.json())
            .subscribe(data => {
            this.abc=data.ContactList;
    }) ,
    err => {
        console.log("Oops!");
    }  
}

#5
this.contactlist = this.contactlist.filter(function(val) {
  return this.abc.indexOf(val) == -1;
});

This will give you the duplicate record.


#6

please tell me @addwebsolution where put this line.


#7

The place where you want to add the duplicate values in the new array.


#8

Sir,i’m frankly speaking.really i don’t understand actually i’m new in ionic 2 that’s why.
if possible can you write me the code.please i request you.


#9
constructor(public navCtrl: NavController, public navParams: NavParams,public contacts:Contacts,public platform: Platform,public http:Http) {

  	 this.platform.ready().then(() => {
       var opts = {   
          ///filter : "M",                                
          multiple: true,        
          hasPhoneNumber:true,                             
          fields:  [ 'displayName', 'name' ]
        };
        this.contacts.find([ 'displayName', 'name' ],opts).then((contacts) => {
          this.contactlist=contacts;
          alert(JSON.stringify(this.contactlist))
        }, (error) => {
          console.log(error);
        })
    })
     this.fetchallcontacts();
     this.combineContacts();
  }


fetchallcontacts(){
           this.http.get('http://xxxx.com/xxxxxx/xxxx/getAllContact.php')
            .map(res => res.json())
            .subscribe(data => {
            this.abc=data.ContactList;
    }) ,
    err => {
        console.log("Oops!");
    }  
}

combineContacts(){
          this.contactlist = this.contactlist.filter(function(val) {
           if(this.abc.indexOf(val) != -1) {
              this.newArray.push(val);
           } 
          });
}

Something like this.


#10

when click on combine contacts button showing an error this.contactlist is undefined.

.ts file

export class HomePage{
contactlist: any;
abc:any;
newArray:any;
  constructor(public navCtrl: NavController, public navParams: NavParams,public contacts:Contacts,public platform: Platform,public http:Http) {

  	 this.platform.ready().then(() => {
       var opts = {   
          ///filter : "M",                                
          multiple: true,        
          hasPhoneNumber:true,                             
          fields:  [ 'displayName', 'name' ]
        };
        this.contacts.find([ 'displayName', 'name' ],opts).then((contacts) => {
          this.contactlist=contacts;
          alert(JSON.stringify(this.contactlist))
        }, (error) => {
          console.log(error);
        })
    })
     this.fetchallcontacts();
  }

ionViewDidLoad() {
 
  }
fetchallcontacts(){
  alert("its working")
           this.http.get('http://xxx.com/aaa/bb/getAllContact.php')
            .map(res => res.json())
            .subscribe(data => {
            this.abc=data.ContactList;
    }) ,
    err => {
        console.log("Oops!");
    }  
}
combineContacts(){
          this.contactlist = this.contactlist.filter(function(val) {
           if(this.abc.indexOf(val) != -1) {
              this.newArray.push(val);
           } 
          });
          alert(JSON.stringify(this.newArray));
}
}

HTML
<button ion-button full color="secondary" (click)="combineContacts()">Combine Contacts</button>
how can i fix it.please help me.


#11

This is because when you are clicking the button, this.contactlist array is not yet filled. So make sure that this.contactlist is filled after that only click on the button.


#12

also i’m checked in real device.but there is also no response.