亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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

迷茫
迷茫

業(yè)精于勤,荒于嬉;行成于思,毀于隨。

reply all(4)
僅有的幸福
// 把數(shù)據(jù)往里面一扔
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