简体   繁体   English

使用从 json 数据读取的循环创建多个 javascript 对象

[英]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.

相关问题 从 MapBox 地理编码 API 检索和使用多个 JSON 数据 - JavaScript 控制台读取“未定义不是对象” - Retrieving and using multiple JSON data from MapBox Geocoding API - JavaScript console reads "undefined is not an object" Javascript 嵌套 for 循环显示来自 JSON 对象数组的数据 - Javascript nested for loop displaying data from JSON objects array JavaScript的新手-需要帮助,以表单中的数据创建多个对象 - New to JavaScript - Need help creating multiple objects with data from a form 从 JSON 数据中循环遍历第一个数组中的多个对象,显示两个 arrays 与使用 FUNCTION 组件的 React 中的对象 - loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT 如何在javascript中使用for循环检索多个JSON对象 - How to retrieve multiple JSON objects with a for loop in javascript 在 for 循环内创建 JavaScript 对象并在不同的 for 循环中使用唯一对象 - Creating JavaScript objects inside of a for loop and using the unique objects in a different for loop 在for循环javascript中创建对象 - creating objects in for loop javascript 如何在 javascript 中使用 for 循环显示来自两个对象的数据 - how display data from two objects using for loop in javascript 使用 JavaScript 从已解析的 JSON 对象中定位特定数据 - Targeting specific data from parsed JSON objects using JavaScript 使用JavaScript或JQuery从json数据创建动态文件夹层次结构 - creating dynamic folder Hierarchy from json data using JavaScript or JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM