[英]how to push the key value in empty object using es6
how to push the empty key value in empty object using es6如何使用es6推送空对象中的空键值
const obj={};
First I need to check the object is isEmpty
or not then I need to push the key and value in that object.首先,我需要检查对象是否为
isEmpty
,然后我需要推送该对象中的键和值。
if(Object.keys(obj).length === 0) {
const contArry = {"name": "xxx", "age": "0"};
}
i tried it but not working for me我试过了,但对我不起作用
My output must be obj={"name": "xxx", "age": "0"}
我的输出必须是
obj={"name": "xxx", "age": "0"}
You could use Object.assign
.你可以使用
Object.assign
。
const obj = {}; if (!Object.keys(obj).length) { Object.assign(obj, { name: "xxx", age: "0" }); } console.log(obj);
You could use Object.assign:你可以使用 Object.assign:
if(Object.keys(obj).length === 0) {
Object.assign(obj, {"name": "xxx", "age": "0"});
}
or just:要不就:
if(Object.keys(obj).length === 0) {
obj.name = "xxx";
obj.age" = "0";
}
how to push the key value in empty object如何在空对象中推送键值
var emp = {};
emp.name = "Steven";
emp.std = "12";
emp.sub = "CS";
emp.school = "LF"
console.log(emp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.