在使用filter()
函数找到要为其分配数据的对象后,尝试将值从一个对象分配给另一个对象。
我没有工作的第一次尝试看起来像这样:
if (this.sets.filter(s => s.id === res.body.last_id).length) {
this.sets.filter(s => s.id === res.body.last_id)[0] = {
id: this.editForm.id,
bid: this.editForm.bid,
budget: this.editForm.budget,
country: this.editForm.country,
desc: this.editForm.desc,
device: this.editForm.device,
profile_id: this.editForm.profile_id,
user_id: this.userId,
widget_list_id: this.editForm.widget_list_id,
widget_type: this.editForm.widget_type
}
}
所以我尝试找到优雅的解决方案,但我能让逻辑工作的唯一方法是:
if (this.sets.filter(s => s.id === res.body.last_id).length) {
this.sets.filter(s => s.id === res.body.last_id)[0].id = this.editForm.id;
this.sets.filter(s => s.id === res.body.last_id)[0].bid = this.editForm.bid;
this.sets.filter(s => s.id === res.body.last_id)[0].budget = this.editForm.budget;
this.sets.filter(s => s.id === res.body.last_id)[0].country = this.editForm.country;
this.sets.filter(s => s.id === res.body.last_id)[0].desc = this.editForm.desc;
this.sets.filter(s => s.id === res.body.last_id)[0].device = this.editForm.device;
this.sets.filter(s => s.id === res.body.last_id)[0].profile_id = this.editForm.profile_id;
this.sets.filter(s => s.id === res.body.last_id)[0].user_id = this.userId;
this.sets.filter(s => s.id === res.body.last_id)[0].widget_list_id = this.editForm.widget_list_id;
this.sets.filter(s => s.id === res.body.last_id)[0].widget_type = this.editForm.widget_type;
}
我显然一切都很优雅。任何想法为什么我的第一次参加不起作用?
答案 0 :(得分:2)
Don't repeat yourself !!!对过滤后的数组使用临时变量(它也只执行一次所有过滤):
const lasts = this.sets.filter(s => s.id === res.body.last_id);
if (lasts.length) {
lasts[0].id = this.editForm.id;
lasts[0].bid = this.editForm.bid;
lasts[0].budget = this.editForm.budget;
lasts[0].country = this.editForm.country;
lasts[0].desc = this.editForm.desc;
lasts[0].device = this.editForm.device;
lasts[0].profile_id = this.editForm.profile_id;
lasts[0].user_id = this.userId;
lasts[0].widget_type = this.editForm.widget_type;
lasts[0].widget_list_id = this.editForm.widget_list_id;
}
此外,还有更多临时变量:
const lasts = this.sets.filter(s => s.id === res.body.last_id);
if (lasts.length) {
const last = lasts[0];
const editForm = this.editForm;
last.id = editForm.id;
last.bid = editForm.bid;
last.budget = editForm.budget;
last.country = editForm.country;
last.desc = editForm.desc;
last.device = editForm.device;
last.profile_id = editForm.profile_id;
last.user_id = this.userId;
last.widget_type = editForm.widget_type;
last.widget_list_id = editForm.widget_list_id;
}
之后,您需要了解有关内置方法的更多信息。 Array find
method过滤掉单个项目(第一个与谓词匹配)和Object.assign
,它将所有可枚举属性从一个对象复制到另一个对象(仅在您的{{ {1}}只包含您要复制的属性。
editForm
答案 1 :(得分:0)
你可以做到
if (this.sets.filter(s => s.id === res.body.last_id).length) {
Object.assign(this.sets.filter(s => s.id === res.body.last_id)[0], {
id: this.editForm.id,
bid: this.editForm.bid,
budget: this.editForm.budget,
country: this.editForm.country,
desc: this.editForm.desc,
device: this.editForm.device,
profile_id: this.editForm.profile_id,
user_id: this.userId,
widget_list_id: this.editForm.widget_list_id,
widget_type: this.editForm.widget_type
})
}