Data structure - How to combine json data into an object array in javascript?
迷茫
迷茫 2017-05-19 10:11:19
0
4
732

like

{"name":"`111","password":"111","title":"111","tag":"111","contents":"1111"},{ "name":"222","password":"22","title":"22","tag":"22","contents":"222"},{"name":"11", "password":"11","title":"11","tag":"11","contents":"11111"}

Converted to

[{"name":"`111","password":"111","title":"111","tag":"111","contents":"1111"}, {"name":"222","password":"22","title":"22","tag":"22","contents":"222"},{"name":"11" ,"password":"11","title":"11","tag":"11","contents":"11111"}]

To add, this is the data transmitted from the backend to the frontend

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (4)
迷茫

    仅有的幸福
    // 把数据往里面一扔 var tx = { a: {"name": "`111", "password": "111", "title": "111", "tag": "111", "contents": "1111"}, b: {"name": "222", "password": "22", "title": "22", "tag": "22", "contents": "222"}, c: {"name": "11", "password": "11", "title": "11", "tag": "11", "contents": "11111"} }; var sb = []; // 遍历栈入 for (var tb in tx) { sb.push(tx[tb]) } console.log(sb[]);
      巴扎黑

      I improved the code upstairs and got the effect the poster wanted

      var obj = { a: {"name": "111", "password": "111", "title": "111", "tag": "111", "contents": "1111"}, b: {"name": "222", "password": "22", "title": "22", "tag": "22", "contents": "222"}, c: {"name": "11", "password": "11", "title": "11", "tag": "11", "contents": "11111"} }; var result = []; for (var key in obj) { result.push(obj[key]); } console.log(JSON.stringify(result));

        Peter_Zhu

        What you mean is:
        Put the data in the .json file sent from the backend into the new object array on the frontend.
        Ajax is required for transmission (an example is the ajax method of jquery). You can also try axios, which is more popular now.

        Assumptions:
        1. The file that needs to be passed in is test.json
        2. The data content of test.json is

        { "userone":{"name":"111","password":"111","title":"111","tag":"111","contents":"1111"}, "usertwo":{"name":"222","password":"22","title":"22","tag":"22","contents":"222"}, "userthree":{"name":"11","password":"11","title":"11","tag":"11","contents":"11111"} }

        Conversion:
        1. Obtain data through ajax, and the obtained content is stored in data
        2. Traverse the json data and save it into a new object array, and perform it in the success function

        function getJson(){ $.ajax({ type:"GET", url:"test.json", dataType:"json", success:function(data){ var jsonData=data; var newObject=[]; for (var key in jsonData) { newObject.push(jsonData[key]) } JSON.stringify(newObject); console.log(newObject); } }) } getJson();
          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!