JavaScript数组与JSON响应的比较 [英] JavaScript array comparison with JSON response

查看:94
本文介绍了JavaScript数组与JSON响应的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在进行的项目,我需要从学校的时间表中获取所有空置房间,并从JSON响应中获取数据.

I have this project I'm working on and I need to get all the vacant rooms from my school's timetable where I get my data from a JSON response.

JSON响应如下:

{
   "status": "success",
   "reservations": [
      {
         "id": "19598",
         "subject": "subjectName",
         "modifiedDate": "2017-04-24T06:04:42",
         "startDate": "2017-04-24T08:00:00",
         "endDate": "2017-04-24T09:45:00",
         "resources": [
            {
               "id": "795",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "599",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "2989",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "41",
               "type": "room",
               "code": "A340.1",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A340.1"
            }
         ],
         "description": ""
      },
      {
         "id": "27832",
         "subject": "subjectName",
         "modifiedDate": "2017-04-24T06:04:42",
         "startDate": "2017-04-24T08:00:00",
         "endDate": "2017-04-24T09:45:00",
         "resources": [
            {
               "id": "52",
               "type": "room",
               "code": "A450.3",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A450.3"
            },
            {
               "id": "2409",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "3401",
               "type": "realization",
               "code": "",
               "name": ""
            }
         ],
         ""
      },
      {
         "id": "10945",
         "subject": "subjectName",
         "modifiedDate": "2017-04-24T06:04:43",
         "startDate": "2017-04-24T08:00:00",
         "endDate": "2017-04-24T12:00:00",
         "resources": [
            {
               "id": "289",
               "type": "student_group",
               "code": "groupCode",
               "name": "gorupName"
            },
            {
               "id": "2454",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "19",
               "type": "room",
               "code": "A510.4",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A510.4"
            }
         ],
         "description": ""
      },
      {
         "id": "27647",
         "subject": "subjectName",
         "modifiedDate": "2017-04-24T06:04:39",
         "startDate": "2017-04-24T08:00:00",
         "endDate": "2017-04-24T21:00:00",
         "resources": [
            {
               "id": "47",
               "type": "room",
               "code": "A420.6",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A420.6"
            }
         ],
         "description": ""
      },
      {
         "id": "20630",
         "subject": "subjectName",
         "modifiedDate": "2017-04-24T06:04:33",
         "startDate": "2017-04-24T08:00:00",
         "endDate": "2017-04-24T10:45:00",
         "resources": [
            {
               "id": "25",
               "type": "room",
               "code": "A130.1",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A130.1"
            },
            {
               "id": "26",
               "type": "room",
               "code": "A130.3",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "A130.3"
            },
            {
               "id": "2997",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2268",
               "type": "student_group",
               "code": "groupCode",
               "name": "gorupName"
            }
         ],
         "description": ""
      },
      {
         "id": "19874",
         "subject": "subjectName",
         "modifiedDate": "2017-04-24T06:04:37",
         "startDate": "2017-04-24T08:00:00",
         "endDate": "2017-04-24T09:45:00",
         "resources": [
            {
               "id": "28",
               "type": "room",
               "code": "A140.2",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "A",
                  "name": "buildingA"
               },
               "name": "140.2"
            },
            {
               "id": "3033",
               "type": "realization",
               "code": "",
               "name": ""
            },
            {
               "id": "2338",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupname"
            }
         ],
         "description": ""
      }
   ]
}

响应时间更长,但为简单起见,我将响应时间缩短了.

The response is a lot longer but I've kept it shorter for simplicity's sake.

因此,我已通过JSON.Parse()for-loops运行此JSON响应,以获取数组中当前正在使用的所有房间;

So I've run this JSON response through with JSON.Parse() and for-loops to get all the rooms that all currently in use in an array;

var rooms = []; 

for (var i = 0; i < json.reservations.length; i++) {
    if(json.reservations[i].resources != null){
        for(var j = 0; j < json.reservations[i].resources.length; j++){
            var resource = json.reservations[i].resources[j];
            if(resource.type === "room"){
                if(rooms.indexOf("code"))
                    rooms.push(resource.code);
                }
            }
        }
    }
}

例如,从上面的响应中可以看到,我得到了当时正在使用的所有房间;

I get all the rooms that are being used at the time as you can see from the response above for example;

"startDate": "2017-04-24T08:00:00",
"endDate": "2017-04-24T09:45:00",

"type": "room",
"code": "A340.1",

但是问题是我正在使用的API目前不包含有关空房间的任何数据,因此我还为buildingA中的所有房间制作了一个数组,如下所示:

But the problem is that the API that I'm using doesn't contain any data for the vacant rooms at the moment so I also made an array for all the rooms in the buildingA which looks like this:

var buildingA = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3',
'A450.1', 'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7',
'A540.1', 'A540.2'];

有什么办法可以将这个数组与var rooms = [];数组进行比较,以便我可以打印所有空房间而不是正在使用的房间?

Is there any way I could compare this array to the var rooms = []; array so I could print all the vacant rooms instead of the ones being in use?

关于结果,我将必须看到空房间的名称以及空房间要停留多长时间的时间(如果可能的话),但是主要的事情是例如获取房间名称;

As for the results I would have to see the name of the vacant room and the time how long it stays vacant (if possible) but the main thing would be to get the room name for example;

A340.1 - 1 hour 45 minutes
A440.4 - 2 hours

谢谢.

推荐答案

基本上,您可以先收集已预订的房间,然后获得完全免费的房间或提供免费的时间.

Basically, you could collect first the rooms which are booked and then get either completely free rooms or render free times.

var data = { status: "success", reservations: [{ id: "19598", subject: "subjectName", modifiedDate: "2017-04-24T06:04:42", startDate: "2017-04-24T08:00:00", endDate: "2017-04-24T09:45:00", resources: [{ id: "795", type: "student_group", code: "groupCode", name: "groupName" }, { id: "599", type: "student_group", code: "groupCode", name: "groupName" }, { id: "2989", type: "realization", code: "", name: "" }, { id: "41", type: "room", code: "A340.1", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "A340.1" }], description: "" }, { id: "27832", subject: "subjectName", modifiedDate: "2017-04-24T06:04:42", startDate: "2017-04-24T08:00:00", endDate: "2017-04-24T09:45:00", resources: [{ id: "52", type: "room", code: "A450.3", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "A450.3" }, { id: "2409", type: "student_group", code: "groupCode", name: "groupName" }, { id: "3401", type: "realization", code: "", name: "" }], description: "" }, { id: "10945", subject: "subjectName", modifiedDate: "2017-04-24T06:04:43", startDate: "2017-04-24T08:00:00", endDate: "2017-04-24T12:00:00", resources: [{ id: "289", type: "student_group", code: "groupCode", name: "gorupName" }, { id: "2454", type: "realization", code: "", name: "" }, { id: "19", type: "room", code: "A510.4", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "A510.4" }], description: "" }, { id: "27647", subject: "subjectName", modifiedDate: "2017-04-24T06:04:39", startDate: "2017-04-24T08:00:00", endDate: "2017-04-24T21:00:00", resources: [{ id: "47", type: "room", code: "A420.6", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "A420.6" }], description: "" }, { id: "20630", subject: "subjectName", modifiedDate: "2017-04-24T06:04:33", startDate: "2017-04-24T08:00:00", endDate: "2017-04-24T10:45:00", resources: [{ id: "25", type: "room", code: "A130.1", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "A130.1" }, { id: "26", type: "room", code: "A130.3", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "A130.3" }, { id: "2997", type: "realization", code: "", name: "" }, { id: "2268", type: "student_group", code: "groupCode", name: "gorupName" }], description: "" }, { id: "19874", subject: "subjectName", modifiedDate: "2017-04-24T06:04:37", startDate: "2017-04-24T08:00:00", endDate: "2017-04-24T09:45:00", resources: [{ id: "28", type: "room", code: "A140.2", parent: { id: "2", type: "building", code: "A", name: "buildingA" }, name: "140.2" }, { id: "3033", type: "realization", code: "", name: "" }, { id: "2338", type: "student_group", code: "groupCode", name: "groupname" }], description: "" }] },
    rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3', 'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3', 'A450.1', 'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7', 'A540.1', 'A540.2'],
    booking = Object.create(null),
    free;


data.reservations.forEach(function (reservation) {
    reservation.resources.some(function (resource) {
        if (resource.type === 'room') {
            booking[resource.code] = booking[resource.code] || [];
            booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
            return true;
        }
    });
});

free = rooms.filter(function (a) {
    return !booking[a];
});
 
console.log(booking);
console.log(free);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于JavaScript数组与JSON响应的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆