JavaScript--往对象里面添加数据
方法一: 直接添加let obj = {name: 'sun'};// []let key = 'age';let value = 18;obj[key] = value;// .obj.age = 18;console.log(obj, 'obj');// {name: 'sun', age: 18} 'obj'方法二: 利用扩展运算符…let objName = {na
·
方法一: 直接添加
let obj = {
name: 'sun'
};
// []
let key = 'age';
let value = 18;
obj[key] = value;
// .
obj.age = 18;
console.log(obj, 'obj');
// {name: 'sun', age: 18} 'obj'
方法二: 利用扩展运算符…
let objName = {
name: 'sun'
};
let objAge = {
age: 18
};
let obj = { ...objName, ...objAge };
console.log(obj, 'obj');
// {name: 'sun', age: 18} 'obj'
方法三: 利用Object.assign()
let obj = {
name: 'sun'
};
let objAge = {
age: 18
};
Object.assign(obj, objAge);
console.log(obj, 'obj');
// {name: 'sun', age: 18} 'obj'
更多推荐
已为社区贡献9条内容
所有评论(0)