lodash:重命名对象中的键
lodash: rename key in object
我想重命名 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"} }
}
你真的不需要lodash。您需要做的是使用旧值在对象上创建一个新键,然后删除旧键。例如:
Object.keys(objs).forEach(function (key) {
objs[key].original_description = objs[key].description;
delete objs[key].description;
});
如果您不熟悉 delete
运算符,请参阅 the MDN documentation for delete
我想重命名 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"} }
}
你真的不需要lodash。您需要做的是使用旧值在对象上创建一个新键,然后删除旧键。例如:
Object.keys(objs).forEach(function (key) {
objs[key].original_description = objs[key].description;
delete objs[key].description;
});
如果您不熟悉 delete
运算符,请参阅 the MDN documentation for delete