我已经编写了使用EventSource获取服务器发送事件的代码,其中包含以下代码。在下面的代码中,我有2个对象,一个是tasksRes,另一个是tasks1。
情况1:因此在其中两个对象中,我必须检查一个ID与另一个ID。如果id与第一个对象匹配,那么我必须在模板中用第二个对象id覆盖第一个对象id。
情况2:如果对象中没有ID,则我必须将这些ID相关的json数据绑定到列表的第一位。
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Checkbox checkbox = (Checkbox)view;
data.get(position).setSelected(checkbox.isChecked);
notifyItemChanged(position); // Important one
}
});
模板代码:
tasks(){
this.handler.activateLoader();
this.tasksService.get(this.page, this.pageSize).subscribe(results => {
this.handler.hideLoader();
if (this.handler.handle(results)) {
return;
}
this.tasksRes = results['data'];
this.length = results['totalElements'];
}, error => {
this.handler.hideLoader();
this.handler.error(error);
});
}
//Get the push task data which updates automatically
getPushData(){
let source = new EventSource(this.url, { withCredentials: true });
source.onopen = function(e){
console.log('The connnection established..');
};
source.addEventListener('message', (e) => {
//let event = JSON.parse(message.data);
let data[] = JSON.parse(e.data);
let data1 = data[1];
this.tasks1 = data1['data'];
console.log(this.tasks1);
});
source.addEventListener('welcome', (e) => {
console.log('welcome event data:', e.data);
});
source.onerror = function(e){
console.log('EventSource failed.');
};
}
这是我从taskRes获得的json数据。
<ul class="p-0" *ngFor="let task of tasksRes">
<li class="pl-2 pr-2 text-dark send-msg mb-1">
<p>
<a href="javascript:;" [routerLink]="[task.link]">
{{ task.name } }
</a> -
<span class="text-muted">{{task.eventType}}</span>
<br/>
<small class="text-muted">{{task.createdDate | timeAgo}} | {{task.status}}
</small>
</p>
</li>
</ul>
这是我从task1对象获取的json数据
{
createdBy:null,
createdDate:"2018-08-20T14:04:27.802+0000",
entityId:"8a8082fb6542fa5f0165475f7d3d2205",
entityType:"Project",
eventType:"Sync",
id:"8a80822c6551bd59016557a6b51a722a",
inactive:false,
link:"/app/projects/8a8082fb6542fa5f0165475f7d3d2205/test-suites",
modifiedBy:null,
modifiedDate:"2018-08-20T14:04:27.802+0000",
name:"chat window",
status: "Done"
}
因此,我想将这两个对象与其ID进行比较,如果找到了它们,则需要使用taskRes与task1进行修改。那么如何实现呢?有什么想法吗?
答案 0 :(得分:0)
Underscore提供了extend()
,它可以满足您的需求...
let tasks1 = // some object
let taskRes = // some object
if (task1.id == taskRes.id) _.extend(tasks1, taskRes) // update tasks1 with the values from matching keys in taskRes