如何在 JavaScript 对象中添加另一个对象
是的,可以将 Object 添加到对象中作为一个属性。可以通过以下方式添加:
const person = {
name: 'John'
};
const address = {
city: 'New York',
country: 'USA'
};
person.address = address;
console.log(person); // output: { name: 'John', address: { city: 'New York', country: 'USA' } }
在这个示例中,我们将一个名为address的 Object 添加到了person 对象中作为一个属性。我们可以通过person.address来访问address 属性的值。
原文地址: https://www.cveoy.top/t/topic/nqww 著作权归作者所有。请勿转载和采集!