【发布时间】:2021-06-24 11:53:22
【问题描述】:
假设我有一个看起来像这样的对象
const randomObj = {
myObj: { id: "1", value: "random string", text: "random text"},
otherObj: { id: "2", value: "random string 2", text: "random text 2"}
}
我想遍历每个对象及其值,但向对象添加一个附加字段
const newObj = { ..._.mapValues(randomObj) }
预期输出
newObj: {
myObj: { id: "1", value: "random string", text: "random text"},
otherObj: { id: "2", value: "random string 2", text: "random text 2"},
}
想要的输出
newObj: {
myObj: { id: "1", value: "random string", text: "random text", objectId: "myObj"},
otherObj: { id: "2", value: "random string 2", text: "random text 2", objectId: "otherObj"},
}
如何添加该附加字段,其中附加字段将是对象的名称?
【问题讨论】:
标签: javascript oop functional-programming lodash