JavaScript 数组与 JSON 响应的比较

2024-01-12

我正在处理这个项目,我需要从学校的时间表中获取所有空房间,我可以从 JSON 响应中获取数据。

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": ""
      }
   ]
}

回复要长得多,但为了简单起见,我将其缩短了。

所以我通过以下命令运行了这个 JSON 响应JSON.Parse() and for-loops获取数组中当前正在使用的所有房间;

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);
                }
            }
        }
    }
}

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

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

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

但问题是,我正在使用的 API 目前不包含任何空房间的数据,所以我还为buildingA看起来像这样:

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 = [];数组以便我可以打印所有空房间而不是正在使用的房间?

至于结果,我必须查看空房间的名称以及空房间的时间(如果可能的话),但主要的是获取房间名称,例如;

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

提前致谢。


基本上,您可以先收集已预订的房间,然后获得完全空闲的房间或渲染空闲时间。

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; }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaScript 数组与 JSON 响应的比较 的相关文章

  • 流媒体性能 - Canvas 与

    我正在开发一个应用程序 需要通过 webSocket 连接以每秒至少 30 帧的速度持续传输图像 我遇到了一些性能问题 并希望尽我所能进行优化 我想知道使用不断更新的图像之间的性能差异是什么 就像这样 img src someDynamic
  • F#:如何将 Json.NET [JsonConstructor] 属性应用于主构造函数?

    我正在尝试用 F 做一些事情 比如JsonConstructorAttribute中的示例Json NET 文档 http www newtonsoft com json help html JsonConstructorAttribute
  • 获取 pdf 第 1 页(共 2 页)的图像

    我正在使用 html2canvas 和 jsPDF 以及 Angular4 创建图像 我想将此图像放置在 2 页生成的 pdf 的第 1 页上 但似乎这条线 doc save test pdf 需要在函数内部htm2canvas 因为如果我
  • 强制执行 show.bind

    我有一个包含数据的表 当从另一个视图触发事件时 我希望视图检查 show bind 语句 问题是该事件没有更改当前视图中的任何数据 foo html tr p canBeRemoved p tr 我正在使用 EventAggregator
  • 尝试使用 CasperJS 跟踪 iframe 中的链接

    我正在尝试使用CasperJS http casperjs org index html跟踪 iframe 中的链接 但我似乎无法获取 iframe 的文档 这是使用我找到的 iframe 示例页面进行的测试 第三个 iframe 有一个名
  • jQuery 中如何判断 JSON 对象是否为空

    我有以下 JSON meta limit 20 next null offset 0 previous null total count 0 objects 我对对象感兴趣 我想知道对象是否为空并显示警报 像这样的东西 success fu
  • 内联执行生成的汇编程序

    我正在阅读以下演示文稿 http wingolog org pub qc 2012 js slides pdf http wingolog org pub qc 2012 js slides pdf其中讨论了 4 10 19 内联 ASM
  • 从对象获取数据 - 我看到数据但无法保存它们

    正如你所看到的 我是新来的 我确实尝试过搜索 但没有找到解决我问题的方法 所以这是我的问题 如果我这样做 console log grid data kendoGrid data 这在控制台中显示如下 所以我明白这一点 有一个数组和一个带有
  • 如何垂直打印数组中的字符串元素? [关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我有一个数组 我想垂直打印每个元素 例如 myArr abc def ghi 输出应该是 a d g b e h c f
  • toJSON() 和 JSON.Stringify() 之间的区别

    如果您需要读取或克隆模型的所有数据属性 请使用其 toJSON 方法 此方法返回属性的副本作为 对象 尽管有其名称 但不是 JSON 字符串 当 JSON stringify 为 使用 toJSON 方法传递一个对象 它将返回的字符串化 t
  • 标记内的值发生变化时调用函数

    JavaScript 有没有什么方法可以在段落标记的值更改时调用函数 概述 HTML p 00 00 p
  • jQuery 如何通过不同的列值计算表中的行数

    如何按表列计算不同的表行 Example table thead tr th NAME th th TECHNOLOGY th tr thead tbody tr td john td td jQuery td tr tr td mark
  • 'DOMException:使用'option:selected'选择器时无法在'Element'上执行'querySelectorAll'

    我正在运行一个页面 该页面在以下行中引发错误 var label select find option selected html select find option first html 为了完整起见 这里是完整的 jQuery 函数
  • 谷歌浏览器如何启动桌面应用程序?

    我真的不知道术语 所以我将从一个例子开始 如果我点击磁力链接 Google Chrome 会询问我是否要启动 torrent 客户端 我单击 确定 chrome 启动该应用程序 该应用程序根据链接执行一些操作 现在有办法查看应用程序如何从
  • 加载 angularjs 路由后运行 javascript 代码

    我需要在 angularjs 加载路线后显示警报 显示警报的代码位于 angularjs 异步加载的视图中 视图加载后 我希望它能够运行 但它没有 我知道我可以广播并告诉它稍后运行等 但我需要一个更通用的解决方案 假设您正在谈论基于以下内容
  • 限制线的长度

    我正在尝试画一条代表 弹弓 的线 并且希望它具有最大拉伸长度 在 p5 中 我在位置和位置之间画了一条线 line posA x posA y posB x posB y posA 是鼠标 x 和 y posB 是画布上圆的位置 我想要做的
  • 如何使用 *non-case* 类通过 Scala 从 JSON 反序列化?

    我正在编写一个 Scala 应用程序 需要序列化和反序列化 JSON 某些 JSON 对象具有超过 22 个字段 因此我无法使用案例类 并且也无法更改格式 我找到的所有 Scala JSON 库都只能 轻松地 与案例类一起使用 而不是与普通
  • 在 Map() 的条目上使用 Promise.all

    我正在使用 Map 来表示一些键 值对 let myMap new Map myMap set foo bar myMap set foo2 bar42 对于每个 Map 条目 我执行一个返回 Promise 的函数 所有这些 Promis
  • Chrome 中的 addEventListener

    我正在关注 Lynda com 上有关新 DOM 事件模型的教程 这是我正在使用的代码 function addEventHandler oNode sEvt fFunc bCapture if typeof window event un
  • javascript:获取带有单位的CSS prop值[重复]

    这个问题在这里已经有答案了 我的代码是这样的 image 1 position absolute top 3vw 我的尝试 http jsfiddle net z8k6t3fb 1 http jsfiddle net z8k6t3fb 1

随机推荐

  • iOS 上的 Chrome 中未触发 .click 事件

    当用户单击移动设备上的菜单栏时 我使用以下代码在我的网站上显示下拉菜单 mobile menu click function nav toggleClass open mobile menu toggleClass open 它在桌面浏览器
  • 使用正则表达式解析有效的父目录

    给定字符串a b c d它代表一个完全限定的子目录我想为父树上的每个步骤生成一系列字符串 即a b c a b and a 使用正则表达式我可以做一个非贪婪的 这会给我匹配的a b and c或贪婪的 这会给我一场比赛a b c 有没有办法
  • 我可以随我的应用程序一起分发 JDK 吗?

    我正在开发一个名为妖娆 http enchanting robotclub ab ca 该应用程序基于Scratch http scratch mit edu 发出 Java 源代码并对其进行编译 以便上传到 LEGO Mindstorms
  • 用于生产时 Sklearn MultiLabelBinarizer() 错误

    编辑 我已更改代码 从 mlb 更改为 TfIdfVectorizer 我仍然面临一个问题 请看下面我的代码 from sklearn externals import joblib from sklearn preprocessing i
  • 配置 Gradle 以将主类和测试类包含到我的 .jar 存档中

    如何配置 Gradle 以将主类和测试类包含到我的 jar 存档中 目前构建的 jar构建 库目录缺少我的测试类 它仅包括来自以下来源的类项目根目录 src main java com packageGradle 将它们编译成后的 jar
  • 如何在应用程序未运行时模拟位置

    我正在构建一个具有地理围栏功能的 iOS 8 应用程序 我需要对此进行测试 并且我知道我可以使用预配置的位置或提供 GPX 文件来模拟 Xcode 中的位置 然而 由于这个应用程序需要在被杀死时根据区域监控进行操作 用户在多任务处理中向上滑
  • 如何使用自定义工厂方法注册开放泛型?

    TL DR 我可以用 Autofac 创建一个通用工厂 以便我可以注入IProduct
  • 将列表列转换为数据框

    我在 Spark 数据框中有一列列表 features 0 45 63 0 0 0 0 0 0 0 85 0 69 0 0 89 56 0 0 0 0 如何将其转换为 Spark 数据框 其中列表中的每个元素都是数据框中的一列 我们可以假设
  • 分析网络软件/具有大量系统调用等待的分析软件

    我正在开发一个复杂的网络软件 但我无法确定如何提高系统性能 特别是在使用阻塞同步调用的软件部分 由于系统的这一部分正在进行大量计算 因此几乎不可能确定该组件的缓慢是由这些计算引起的还是由于等待系统的其他部分引起的 是否有任何轻量级分析器可以
  • 在模式推断解析 json 中检测到格式错误的记录

    我在尝试使用 ADF 中的数据集解析从 Blob 存储读取的基本 Json 时遇到一个非常令人沮丧的错误 我的 Json 如下 Bid 0 197514880839 BaseCurrency AED Bid 0 535403560434 B
  • 使用两个下拉按钮构建一个散点图,一个用于 x 轴,一个用于 y 轴

    我希望构建一个散点图 但使用 2 个下拉列表 我可以在一个用于 x 轴的变量和另一个用于 y 轴的变量之间切换 例如总测试和总案例 或最近案例和总案例 我尝试构建一个我通过替换找到的解决方案 cols dd Total tests Tota
  • SQLAlchemy 声明式:没有任何主键的表?

    如何使用ORM声明类创建没有主键的表 如果我没有制作任何专栏 它就无法启动primary key True SQLAlchemy 核心不会抱怨缺少主键 因此可以使用以下命令创建这样的表Table 但是ORM在设计上需要一种方法来识别对象对应
  • setColorFilter 不起作用

    我正在尝试在图像视图上实现一个简单的滤色器 将黑色图像变成白色图像 为了实现这一目标 我执行以下操作 weatherImg setColorFilter Color BLACK PorterDuff Mode MULTIPLY weathe
  • 异常类 - What() 函数

    我目前正在开发自己的异常类 该类继承自std exception 我不确定我是否应该自己做what 或者只是打电话std exception message 在我的类构造函数中 这是我当前的代码 FilterException Filter
  • 如何设置字体大小或标签大小以适合所有设备

    如何设置 kivy 字体大小或标签大小 使其适合所有手机设备屏幕 适合的方式是不与边界 矩形 甚至屏幕重叠 我知道 Kivy 中的按钮和其他一些小部件有size hint This size hint没有给出想要的结果 另外 设置font
  • 如何导入npgsql模块?

    我需要从 PowerShell 脚本中连接到 PostgreSQL 数据库 但是如何在没有 VisualStudio 的情况下安装 npgsql 呢 没有金块 所以我尝试使用最新的 MSI 文件在 GAC 中安装驱动程序 Npgsql 3
  • 尝试在 VS Code 中将 emmet 与 React 应用程序一起使用

    我正在使用 Visual Studio Code 并创建一个 React 应用程序 我知道 Emmet 附带了 Vsc 但它不适用于我的 React 应用程序 我尝试将以下代码放入设置中 emmet includeLanguages jav
  • 图像Python3的中值滤波器

    I wanted to implement a radial median filter I have the following picture size Nx Ny 我想导出每个像素的半径 对于每个半径计算中值并将其放入一个新矩阵中以代
  • 如何读取 ASP.NET 原始 URL 的查询字符串参数?

    我有一个变量 string rawURL HttpContext Current Request RawUrl 如何读取此 url 的查询字符串参数 这可能就是你想要的 Uri theRealURL new Uri HttpContext
  • JavaScript 数组与 JSON 响应的比较

    我正在处理这个项目 我需要从学校的时间表中获取所有空房间 我可以从 JSON 响应中获取数据 JSON 响应如下所示 status success reservations id 19598 subject subjectName modi