[英]creating multiple javascript objects using a loop that reads from json data
I am trying to create a number of Javscript objects at once by using a loop through json data.我正在尝试通过使用 json 数据循环来一次创建多个 Javscript 对象。 I'm having trouble finding a way to do this and if someone could take a look at the below code and suggest the best way of looping through the json data and creating the objects that would be great.我很难找到一种方法来做到这一点,如果有人可以看看下面的代码并建议循环遍历 json 数据和创建对象的最佳方法,那将是很棒的。
// Create Test Constructor
function Test(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
}
// Create Test Object
let test1 = new Test("name[0]", age[0], "address[0]");
//json data from a seperate .json file
{
"Tests": [
{
"name": "First Person",
"age": 20,
"address": "New York"
},
{
"name": "Second Person",
"age": 21,
"address": "The World"
},
{
"name": "Third Person",
"age": 22,
"address": "The Universe"
}
]
}
Thanks谢谢
If you just need plain objects:如果您只需要普通对象:
const { Tests } = JSON.parse('//json data from a seperate .json file');
Now Tests
should be an array of Test
objects.现在Tests
应该是一组Test
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.