[英]Angular5: Build json from values
I am trying to build a json from values so that I can push it to an object. 我正在尝试从值构建一个json,以便可以将其推送到对象。 I have two items as below.
我有以下两项。
var item1 = "2"
var item2 = "Student"
I am trying to build a json as below 我正在尝试构建如下的json
{ "2" : "student"}
I tried couple of ways but ran out of ideas. 我尝试了几种方法,但没有想法。 Is there an easy way or any lodash method to do it.
有没有简单的方法或任何lodash的方法来做到这一点。
使用ES6计算的属性,例如:
{ [item1]: item2 }
create a Array temp variable and put them on it like this: 创建一个数组临时变量并将其放在上面,如下所示:
var item1 = "2"
var item2 = "Student"
let tmp = [item1, item2];
console.log(JSON.stringify(tmp));
after stringify result is : 字符串化后的结果是:
["2","Student"]
or this on : 或这个:
var student = "2";
console.log(JSON.stringify({student}));
result is: 结果是:
{"student":"2"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.