JavaScript数组与JSON响应的比较
我有一个我正在做的项目,我需要从学校的时间表中获取所有的空房,我从JSON回复中获取数据 JSON响应如下所示:JavaScript数组与JSON响应的比较,javascript,arrays,json,api,compare,Javascript,Arrays,Json,Api,Compare,我有一个我正在做的项目,我需要从学校的时间表中获取所有的空房,我从JSON回复中获取数据 JSON响应如下所示: { "status": "success", "reservations": [ { "id": "19598", "subject": "subjectName", "modifiedDate": "2017-04-24T06:04:42", "startDate": "2017-04-
{
"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": ""
}
]
}
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'];
响应要长得多,但为了简单起见,我把它缩短了
因此,我使用JSON.Parse()
和for循环运行了这个JSON响应,以获取数组中当前使用的所有房间
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);
}
}
}
}
}
但问题是,我使用的API目前不包含任何空房间的数据,因此我还为buildingA
中的所有房间制作了一个数组,如下所示:
{
"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": ""
}
]
}
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=[]进行比较代码>数组,这样我就可以打印所有的空房间,而不是正在使用的房间
至于结果,我必须查看空置房间的名称以及空置的时间(如果可能的话),但最重要的是获取房间名称
"startDate": "2017-04-24T08:00:00",
"endDate": "2017-04-24T09:45:00",
"type": "room",
"code": "A340.1",
A340.1 - 1 hour 45 minutes
A440.4 - 2 hours
提前感谢。基本上,您可以先收集已预订的房间,然后获得完全免费的房间或提供免费时间
var数据={状态:“成功”,保留:[{id:“19598”,主题:“subjectName”,修改日期:“2017-04-24T06:04:42”,起始日期:“2017-04-24T08:00:00”,结束日期:“2017-04-24T09:45:00”,资源:[{id:“795”,类型:“学生组”,代码:“组码”,名称:“组名:},{id:“599”,类型:“学生组”,代码:“组码”,名称:“groupName”}、{id:“2989”、类型:“实现”、代码:“、名称:”}、{id:“41”、类型:“房间”、代码:“A340.1”、家长:{id:“2”、类型:“建筑”、代码:“A”、名称:“buildingA”}、名称:“A340.1”}、描述:}、{id:“27832”、主题:“主题名称”,修改日期:“2017-04-24T06:04:42”,起始日期:“2017-04-24T08:00”,结束日期:2017-04-24T09:45:00”,参考资料:[{id:“52”,类型:“房间”,代码:“A450.3”,家长:{id:“2”,类型:“建筑”,代码:“A”,名称:“建筑”,名称:“A450.3”},{id:“2409”,类型:“学生组”,代码:“组码”,名称:“组名”},{id:“3401”,类型:“实现”,代码:,“名称:”},描述:},{id:“10945”,主题:主题名称”,修改日期:“2017-04-24T06:04:43”,起始日期:“2017-04-24T08:00:00”,结束日期:“2017-04-24T12:00:00”,资源:[{id:“289”,类型:“学生组”,代码:“组码”,名称:“gorupName”},{id:“实现”,代码:,名称:},{id:“19”,类型:“房间”,代码:“A510.4”,家长:{id:“2”,类型:“建筑”,代码:“A,名称:“buildingA”},名称:“A510.4”},描述:“},{id:“27647”,主题:“subjectName”,修饰日期:“2017-04-24T06:04:39”,起始日期:“2017-04-24T08:00:00”,结束日期:“2017-04-24T21:00:00”,资源:[{id:“47”,类型:“房间”,代码:“A420.6”,父项:{id:“2”,类型:“building”,代码:“A”,名称:“buildingA”},名称:“A420.6”],描述:“},{id:“20630”,主题:“subjectName”,修改日期:“2017-04-24T06:04:33”,起始日期:“2017-04-24T08:00:00”,结束日期:“2017-04-24T10:45:00”,资源:[{id:“25”,类型:“房间”,代码:“A130.1”,父项:{id:“2”,类型:“建筑”,代码:“A”,名称:“buildingA”},名称:“A130.1”},{id:“26”,类型:“房间”,代码:“A130.3”,家长:{id:“2”,类型:“building”,代码:“A”,名称:“buildingA”},名称:“A130.3”},{id:“2997”,类型:“realization”,代码:“,名称:”},{id:“2268”,类型:“student_group”,代码:“groupCode”,名称:“gorupName”},描述:},{id:“19874”,主题:“subjectName”,修饰日期:“2017-04-24T06:04:37”,起始日期:“2017-04-24T08:00:00”,结束日期:“2017-04-24T09:45:00”,资源:[{id:“28”,类型:“房间”,代码:“A140.2”,家长:{id:“2”,类型:“建筑”,代码:“A”,名称:“buildingA”},名称:“140.2”},{id:“实现”,代码:,名称:“},{id:“2338”,类型:“学生组”,代码:“组代码:“组代码”,名称:“组名:”},描述:},
房间=['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.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.4.4','A520.5','A520.5',',“A540.1”,“A540.2'],
booking=Object.create(空),
自由的
data.reservations.forEach(函数(预订){
reservation.resources.some(函数(资源){
如果(resource.type==='room'){
预订[resource.code]=预订[resource.code]| |[];
booking[resource.code].push({startDate:reservation.startDate,endDate:reservation.endDate});
返回true;
}
});
});
免费=房间。过滤器(功能(a){
返回!预订[a];
});
控制台日志(预订);
console.log(免费);
.as控制台包装{max height:100%!important;top:0;}
希望下面的内容有助于筛选数组
var vacantRooms = buildingA.filter((x) => {return !rooms.find((y) => {return y == x})});
结果应该是什么样的?你只想得到数组中的差异,就像这个答案一样?如果rooms
也以同样的方式拥有对象,那么一个快速的方法就是var diffArr=buildingA.filter(x=>!(新的集合(rooms)).has(x))
。这将产生一定的差异。但您可以循环并执行相同的操作。空闲时间需要所有可能的预订时间。谢谢!我现在有空房间代码。唯一剩下的就是为它们获取时间戳。