Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6将值从一个对象分配到另一个对象_Javascript_Ecmascript 6 - Fatal编程技术网

Javascript ES6将值从一个对象分配到另一个对象

Javascript ES6将值从一个对象分配到另一个对象,javascript,ecmascript-6,Javascript,Ecmascript 6,使用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.editFor

使用
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;
}
我很清楚这一切,但不优雅。知道我第一次尝试失败的原因吗?

你可以

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
    })
}
你可以

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
    })
}
!!!为筛选数组使用临时变量(该数组也只执行一次所有筛选):

此外,还有更多的临时变量:

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;
}
在此之后,您需要更多地了解内置方法。有一种方法可以过滤出单个项(与谓词匹配的第一个项),并将所有可枚举属性从一个对象复制到另一个对象(仅当
editForm
仅具有要复制的属性时,该方法才有效)

!!!为筛选数组使用临时变量(该数组也只执行一次所有筛选):

此外,还有更多的临时变量:

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;
}
在此之后,您需要更多地了解内置方法。有一种方法可以过滤出单个项(与谓词匹配的第一个项),并将所有可枚举属性从一个对象复制到另一个对象(仅当
editForm
仅具有要复制的属性时,该方法才有效)


为什么不使用扩展运算符使用单行代码
this.sets.filter(s=>s.id==res.body.last_id)[0]={……this.editForm}
。如果你想让它少一些实验性,可以使用
Object.assign()
来代替:
this.sets.filter(s=>s.id==res.body.last_id)[0]=Object.assign({},this.editForm)
为什么不使用扩展操作符使用一行代码呢
this.sets.filter(s=>s.id==res.body.last_id)[0]={……this.editForm}
。如果您想让它少一些实验性,可以使用
Object.assign()
代替:
this.sets.filter(s=>s.id==res.body.last\u id)[0]=Object.assign({},this.editForm)
这可以在一行代码中完成
this.sets.filter(s=>s.id==res.body.last_id)[0]=Object.assign({},this.editForm)
@connexo如果不起作用,它将得到与OP尝试的结果相同的结果,他可以做
Object.assign(this.sets.filter==res.body.last_id)[0],this.editForm)
,,但是需要手动添加
user\u id
,因为它来自
this.userId
,而不是
this.editForm.user\u id
,这可以在一行代码中完成
this.sets.filter(s=>s.id==res.body.last_id)[0]=Object.assign({},this.editForm)
@connexo如果不起作用,它将得到与OP尝试的结果相同的结果,他可以做
Object.assign(this.sets.filter==res.body.last_id)[0],this.editForm)
,,但是需要手动添加
user\u id
,因为它来自
this.userId
,而不是
this.editForm.user\u id