JSON数组值用于在javascript中分隔列表

时间:2017-09-25 10:59:10

标签: javascript jquery arrays json arraylist

我有一个关于转换JSON数组的问题。被困了几个小时,无法搞清楚。我有一个像这样的JSON数组:

list1 = [1, "Marcel", "15:00:00", "21:00:00", "21-09-2017", 38, "3831BC", 2, "", "Leusden", 135.13]

我尝试为每个对象添加一个单独的列表。 所以对于第一个对象:

$.get("restservices/salaries", function(data) {
    for (var i = 0; i < data.length; i++) {
        a.push(data[i].client);
        console.log(a);
    }
})

我试过了:

["Marcel", "Peter", "Marcel"]

但是,它将所有对象的值放在1个列表中,输出为

["Marcel"]
["Peter"]
["Marcel"]

但我想要的是:

ReportType | ResourceId
  t2         123
  t3           5

2 个答案:

答案 0 :(得分:4)

您应该使用Object.values方法来获得满足您需求的简单解决方案。

array=array.map(function(item){
   return Object.values(item);
});

let array=[ { id: 1, client: "Marcel", starttime: "15:00:00", endtime: "21:00:00", date: "22-09-2017", weeknumber: 38, zipcode: "3831BC", housenumber: 2, hncharacter: "", place: "Leusden", Salary: 135.13 }, { id: 2, client: "Peter", starttime: "13:00:00", endtime: "19:30:00", date: "23-09-2017", weeknumber: 38, zipcode: "3831BC", housenumber: 2, hncharacter: "", place: "Leusden", Salary: 194.87 }, { id: 3, client: "Marcel", starttime: "01:45:00", endtime: "09:00:00", date: "24-09-2017", weeknumber: 38, zipcode: "3831BC", housenumber: 2, hncharacter: "", place: "Leusden", Salary: 252.01 }];
array=array.map(function(item){
    return Object.values(item);
});
console.log(array);

答案 1 :(得分:0)

你也可以尝试一下。

    let users = [{"id": 1,"client": "Marcel","starttime": "15:00:00","endtime": "21:00:00","date": "22-09-2017","weeknumber": 38,"zipcode": "3831BC","housenumber": 2,"hncharacter": "","place": "Leusden","Salary": 135.13},
    {"id": 2,"client": "Peter","starttime": "13:00:00","endtime": "19:30:00","date": "23-09-2017","weeknumber": 38,"zipcode": "3831BC","housenumber": 2,"hncharacter": "","place": "Leusden","Salary": 194.87},
    {"id": 3,"client": "Marcel","starttime": "01:45:00","endtime": "09:00:00","date": "24-09-2017","weeknumber": 38,"zipcode": "3831BC","housenumber": 2,"hncharacter": "","place": "Leusden","Salary": 252.01}]
function demo() {
    let result = [];
    for (let index = 0; index < users.length; index++) {
        let details = [];
        for (let key of Object.keys(users[index])) {
            details.push(users[index][key])
        }
        result.push(details);
    }
    console.log(result);
}
demo();

    let users = [{"id": 1,"client": "Marcel","starttime": "15:00:00","endtime": "21:00:00","date": "22-09-2017","weeknumber": 38,"zipcode": "3831BC","housenumber": 2,"hncharacter": "","place": "Leusden","Salary": 135.13},
        {"id": 2,"client": "Peter","starttime": "13:00:00","endtime": "19:30:00","date": "23-09-2017","weeknumber": 38,"zipcode": "3831BC","housenumber": 2,"hncharacter": "","place": "Leusden","Salary": 194.87},
        {"id": 3,"client": "Marcel","starttime": "01:45:00","endtime": "09:00:00","date": "24-09-2017","weeknumber": 38,"zipcode": "3831BC","housenumber": 2,"hncharacter": "","place": "Leusden","Salary": 252.01}]
    function demo() {
        let result = [];
        for (let index = 0; index < users.length; index++) {
            let details = [];
            for (let key of Object.keys(users[index])) {
                details.push(users[index][key])
            }
            result.push(details);
        }
        console.log(result);
    }
    demo();