javascript - ES6 将一个对象的值分配给另一个对象

标签 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.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;
}

我显然一切都那么优雅。知道为什么我的第一次尝试没有成功吗?

最佳答案

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 ,它将所有可枚举属性从一个对象复制到另一个对象(仅当您的 editForm 仅具有您要复制的属性时才有效)。

const last = this.sets.find(s => s.id === res.body.last_id);
if (last) {
    Object.assign(last, this.editForm);
    last.user_id = this.userId;
}

关于javascript - ES6 将一个对象的值分配给另一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48971886/

相关文章:

php - 通过在 php 中选择第一个下拉列表值来填充第二个下拉列表

javascript - 根据复选框输入过滤深层对象

javascript - 如何在 React 应用程序中访问 this.refs 不可用的函数中的 DOM 元素?

javascript - Webpack 配置 - 添加另一个入口文件

javascript - 图像未上传到nodejs中的数据库

javascript - 如何使用 webpack 从 node_modules 加载脚本

javascript - 另存为与 CSS 混合的图像 Canvas

javascript - 何时对正则表达式使用构造函数或文字?

typescript - 在 TS 1.7 中重新导出 ES6 模块?

javascript - 如何在 ecmascript-6 中导入模块?