我必须检索一个对象并将其分配给另一个对象。但是在新对象中,总是仅存储最后一个项目。
我尝试了object.entries键方法,但没有任何效果。 代码:https://codesandbox.io/s/k3p7j07x8o
let subject = {};
const obj = { a: 5, b: 4, c: 9 };
for (const [key, value] of Object.entries(obj)) {
console.log(`${key} ${value}`);
//console.log(subject); // Here itself it's printing the last element
subject.value = key;
subject.text = value;
console.log(subject.value + subject.text); // This showing the correct element
console.log(subject); // This one showing the last elements
}
答案 0 :(得分:0)
您可以使用以下语法:
let subject = Object.assign({}, obj}
console.log(subject)
或
let subject = {};
Object.assign(subject, obj);
console.log(subject)