0
我需要比较对象数组的元素,并且如果数组#1中的对象存在于数组#2上,则返回任何内容,但如果对象不存在,则将对象到阵列#2。我使用打字稿的角度。ForEach循环错过比较
//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."
}]
但总是给出与“不存在!!!”相同的结果。或者总是“存在!!!”
欢迎您:)别忘了把你的答案标记为答案:)我遗憾的是没有时间再次看到它,我不知道为什么其他用户删除它:( – Icepickle