2017-02-28 1 views
0

オブジェクトの配列と要素を比較する必要があります。配列#1のオブジェクトが配列#2に存在する場合は何も返しませんが、オブジェクトが存在しない場合はオブジェクトをアレイ#2に出力する。 Imはtypescriptで角度を使用しています。For Eachループは比較を逃します

//Here i´m just open a confirmation windows 
    public addSelectedContacts(ev: any): void 
    { 
    let confirm = this.$mdDialog.confirm() 
     .title('Are you sure want to add the selected contacts?') 
     .htmlContent('<b>' + this.selectedContacts.length + ' selected</b>' + ' will be add to this Appplication.') 
     .ariaLabel('add contacts') 
     .targetEvent(ev) 
     .ok('OK') 
     .cancel('CANCEL'); 

    this.$mdDialog.show(confirm).then(()=> 
    { 
//Here is whent i´m try to compare the arrays after confirm the action 
     this.selectedContacts.forEach((contact:any)=>{ 
     if(this.application.clients.indexOf(contact) != -1){ 
      console.log('Exists!!!!'); 
     }else{ 
      console.log('Not Exists!!!!'); 
      this.applications.clients.push(contact) 
     } 
     }); 
     this.selectedContacts = []; 
    }); 
    } 

//They arrays will something like this 
this.application.clients = [ 
     { 
      "id": "5725a680b3249760ea21de52", 
      "name": "Abbott", 
      "lastName": "Keitch", 
      "avatar": "assets/images/avatars/Abbott.jpg", 
      "nickname": "Royalguard", 
      "company": "Saois", 
      "jobTitle": "Digital Archivist", 
      "email": "[email protected]", 
      "phone": "+1-202-555-0175", 
      "sex":"Male", 
      "country":"Germany", 
      "website_url":"http://google.com", 
      "publisher":"Michael Smith", 
      "channel":"online", 
      "linkedin_url":"http://linkedin.com", 
      "xing_url":"http://xing.com", 
      "notes": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio modi quos perspiciatis voluptate debitis voluptas." 
     }, 
     { 
      "id": "5725a680606588342058356d", 
      "name": "Arnold", 
      "lastName": "Matlock", 
      "avatar": "assets/images/avatars/Arnold.jpg", 
      "nickname": "Wanderer", 
      "company": "Laotcone", 
      "jobTitle": "Graphic Artist", 
      "email": "[email protected]", 
      "phone": "+1-202-555-0141", 
      "sex":"Male", 
      "country":"Germany", 
      "website_url":"http://google.com", 
      "publisher":"Michael Smith", 
      "channel":"online", 
      "linkedin_url":"http://linkedin.com", 
      "xing_url":"http://xing.com", 
      "notes": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio modi quos perspiciatis voluptate debitis voluptas." 
     }, 
     { 
      "id": "5725a6808a178bfd034d6ecf", 
      "name": "Mai", 
      "lastName": "Nox", 
      "avatar": "assets/images/avatars/Mai.jpg", 
      "nickname": "Violetmage", 
      "company": "quadzone", 
      "jobTitle": "Software Engineer", 
      "email": "[email protected]", 
      "phone": "+1-202-555-0199", 
      "sex":"Male", 
      "country":"Germany", 
      "website_url":"http://google.com", 
      "publisher":"Michael Smith", 
      "channel":"online", 
      "linkedin_url":"http://linkedin.com", 
      "xing_url":"http://xing.com", 
      "notes": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio modi quos perspiciatis voluptate debitis voluptas." 
     }] 
this.selectedContacts = [ 
     { 
      "id": "5725a6808a178bfd034d6ecf", 
      "name": "Mai", 
      "lastName": "Nox", 
      "avatar": "assets/images/avatars/Mai.jpg", 
      "nickname": "Violetmage", 
      "company": "quadzone", 
      "jobTitle": "Software Engineer", 
      "email": "[email protected]", 
      "phone": "+1-202-555-0199", 
      "sex":"Male", 
      "country":"Germany", 
      "website_url":"http://google.com", 
      "publisher":"Michael Smith", 
      "channel":"online", 
      "linkedin_url":"http://linkedin.com", 
      "xing_url":"http://xing.com", 
      "notes": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio modi quos perspiciatis voluptate debitis voluptas." 
     }, 
     { 
      "id": "5725a68009e20d0a9e9acf2a", 
      "name": "Barrera", 
      "lastName": "Bradbury", 
      "avatar": "assets/images/avatars/Barrera.jpg", 
      "nickname": "Jackal", 
      "company": "Unizim", 
      "jobTitle": "Graphic Designer", 
      "email": "[email protected]", 
      "phone": "+1-202-555-0196", 
      "sex":"Female", 
      "country":"Germany", 
      "website_url":"http://google.com", 
      "publisher":"Michael Smith", 
      "channel":"online", 
      "linkedin_url":"http://linkedin.com", 
      "xing_url":"http://xing.com", 
      "notes": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio modi quos perspiciatis voluptate debitis voluptas." 
     }, 
     { 
      "id": "5725a6809fdd915739187ed5", 
      "name": "Blair", 
      "lastName": "Strangeway", 
      "avatar": "assets/images/avatars/Blair.jpg", 
      "nickname": "Knight", 
      "company": "Conedubdax", 
      "jobTitle": "Visual Designer", 
      "email": "[email protected]", 
      "phone": "+1-202-555-0118", 
      "sex":"Female", 
      "country":"Germany", 
      "website_url":"http://google.com", 
      "publisher":"Michael Smith", 
      "channel":"online", 
      "linkedin_url":"http://linkedin.com", 
      "xing_url":"http://xing.com", 
      "notes": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio modi quos perspiciatis voluptate debitis voluptas." 
     }] 

しかし、常に「存在しない!!!」と同じ結果を返します。または常に「Exists !!!」

答えて

1

これまでの回答のおかげで、私はあなたの答えに感謝しましたが、以前に投稿されたコードがありました。

this.$mdDialog.show(confirm).then(()=> 
    { 
     this.selectedContacts.forEach((contact:any)=>{ 
     let exist = false; 
     this.application.clients.forEach((client:any) => { 
      if (client === contact) { 
      exist = true; 
      } 
     }); 
     if(exist){ 
      return; 
     }else{ 
      this.application.clients.push(contact) 
     } 
     }); 
     this.selectedContacts = []; 
    }); 

 if (client.id === contact.id) { 
     exist = true; 
     } 

ため

 if (client === contact) { 
     exist = true; 
     } 

を変更した場合にのみdiferentあなたのアイデアや助けを皆さんありがとう:D

+0

あなたは歓迎:)しているではないですか答えとしてあなたの答えをマークすることを忘れないでください:)私は悲しいことに、もう一度それを見る時間がなかったと私は他のユーザーがそれを削除した理由を知らない:( – Icepickle

関連する問題