javascript - 在node.js中读取文件是否保证项目的顺序

标签 javascript node.js express node-modules

在我的 node.js 应用程序中,我读取了测试数据文件来填充一些输入。测试文件包含一个对象数组。

我用来阅读:

data = fs.readFileSync(fileName, "utf8");

我的测试文件:

[
 {
  "firstname": "John",
  "lastname": "Doe",
  "birthdate": "01/01/1970"
 },
 {
  "firstname": "El",
  "lastname": "Maestro",
  "birthdate": "01/01/1989",
  "isDeleted": true
 }
]

所以问题是 - 当我读取此文件时,它是否保证我将始终在索引 0 处获得名称为“John”的对象,在索引 1 处获得名称为“El”的对象?

最佳答案

数组始终确保 JS 中的顺序

但是

对象内部的键不能确保 JS 中的顺序,它们是“无序键值对”

回答你的问题:

when I read this file is it guaranteed that I will always get object with name "John" at index 0, and "El" at index 1

是的

但是

结果对象中的键可以是无序的,例如:它可以是

{
  "firstname": "John",
  "lastname": "Doe",
  "birthdate": "01/01/1970"
 }

{
  "lastname": "Doe",
  "birthdate": "01/01/1970",
  "firstname": "John"
 }

等...

关于javascript - 在node.js中读取文件是否保证项目的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57465192/

相关文章:

javascript - 是否有可能执行从与梦魇网站的JavaScript编写的功能?或任何其他方式?

javascript - nvd3捕获堆积面积图上的点击事件

javascript - 通过 API 更新 DialogFlow Agent 的实体条目列表

ruby - 如何在 node.js 中执行此 PKCS7 签名?

node.js - 曾经将 RabbitMQ 和 Node.js 用于长时间运行的进程吗?

javascript - 使用 NodeJS 将卡添加到 Stripe 客户

node.js - 在 node.js express 中访问 HTTP 服务器对象

javascript - jquery : create xpath of the current text selection

node.js - 使用 Swig 渲染静态文件

javascript - 如何在 _.flatMap 循环中添加条件并使用 reacJS 将其附加到 className 中?