[英]How to append or add string to existing property in an object?
a={
propA="abcd@x.com",
propb="123",
propc="USA"}
b={
propA="xyz@x.com",
propb="566",
propc="Ind"}
If I have to append PropA of object b to propA of a, what should I do ?如果我必须将对象 b 的 PropA 附加到 a 的 propA,我该怎么办?
expected output: it can be array or new object预期输出:它可以是数组或新对象
a={
propA=["abcd@x.com","xyz@x.com"]
propb="123",
propc="USA"}
You could wrap it in square bracket []
你可以用方括号
[]
把它包起来
const a = { propA: "abcd@x.com", propb: "123", propc: "USA", } const b = { propA: "xyz@x.com", propb: "566", propc: "Ind", } a.propA = [a.propA, b.propA] console.log(a)
let yourKey = 'propA'; // in your case you want to update propA
a[yourKey] = [a[yourKey], b[yourKey]];
console.log(a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.