【问题标题】:lodash: rename key in object [duplicate]lodash:重命名对象中的键[重复]
【发布时间】:2016-09-04 14:42:44
【问题描述】:

我想重命名 obj 中的键,从这里开始:

objs = {
  one: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} },
  two: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }
}

进入这个:

objs = {
  one: { original_description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} },
  two: { original_description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }
}

【问题讨论】:

    标签: javascript object lodash


    【解决方案1】:

    您实际上并不需要 lodash。您需要做的是使用旧值在对象上创建一个新键,然后删除旧键。例如:

    Object.keys(objs).forEach(function (key) {
        objs[key].original_description = objs[key].description;
        delete objs[key].description;
    });
    

    如果您不熟悉 delete 运算符,请参阅 the MDN documentation for delete

    【讨论】:

    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多