我需要比较对象数组的元素,如果数组#1中的对象存在于数组#2上,则不返回任何内容,但如果对象不存在则将对象推入数组#2 。我使用angular with typescript。
//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": "abbott@withinpixels.com",
"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": "arnold@withinpixels.com",
"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": "mai@withinpixels.com",
"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": "mai@withinpixels.com",
"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": "barrera@withinpixels.com",
"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": "blair@withinpixels.com",
"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."
}]
但总是给出与“不存在!!!”相同的结果或者总是“存在!!!”
答案 0 :(得分:0)
我能找到答案,感谢大家在此之前的回答,感谢你的回答,即使你删除它,但这里是之前发布的代码。
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 === contact) {
exist = true;
}
代表
if (client.id === contact.id) {
exist = true;
}
谢谢你们的意见和帮助:D