简体   繁体   中英

How to create JSON or Array of Objects From Three JS Arrays

How to load into/generate a JSON object or an Array of Objects like below

var players = [
        {"name":"John", "startPoint":"3", "maxPoint":"80"}, 
        {"name":"Anna", "startPoint":"6", "maxPoint":"28"}, 
        {"name":"Peter", "startPoint":"6", "maxPoint":"80"},
        {"name":"Zack", "startPoint":"2", "maxPoint":"65"}, 
        {"name":"Andy", "startPoint":"4", "maxPoint":"80"}, 
        {"name":"Mick", "startPoint":"0", "maxPoint":"65"}, 
        {"name":"Robin", "startPoint":"2", "maxPoint":"80"}, 
        {"name":"Ed", "startPoint":"5", "maxPoint":"28"}, 
        {"name":"Chris", "startPoint":"1", "maxPoint":"65"}, 
        {"name":"Lee", "startPoint":"3", "maxPoint":"80"}, 
        {"name":"Baron", "startPoint":"6", "maxPoint":"65"}, 
        {"name":"Sam", "startPoint":"1", "maxPoint":"80"}   
];

from three JavaScript Array Object as:

var members = ["John","Anna","Peter","Zack","Andy","Mick","Robin","Ed","Chris","Lee","Baron" ,"Sam" ]
var startPoint = [3, 6, 6, 2, 4, 0, 2, 5, 1, 3, 6, 1];
var maxPoint =[80, 28, 80, 65, 80, 65, 80, 80, 65, 80, 65, 80];
var result = [], i;
for (i = 0; i < members.length; ++i) {
    result.push({name: members[i], startPoint: startPoint[i], maxPoint: maxPoint[i]});
}
return JSON.stringify(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM