简体   繁体   English

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

[英]Does reading file in node.js guarantee the order of items

In my node.js application I have test data file that I read to populate some inputs.在我的 node.js 应用程序中,我读取了测试数据文件以填充一些输入。 The test file contains an array of objects.测试文件包含一组对象。

I use for reading:我用于阅读:

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

My test file:我的测试文件:

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

So the question is - 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?所以问题是 - 当我阅读这个文件时,是否保证我将始终在索引 0 处获得名为“John”的对象,在索引 1 处获得名为“El”的对象?

Arrays always ensures order in JS数组始终确保 JS 中的顺序

BUT

Keys inside objects doesnot ensure order in JS, they are 'unordered key value pair'对象内的键并不能确保 JS 中的顺序,它们是“无序键值对”

To answer your question:回答你的问题:

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当我阅读这个文件时,是否保证我将始终在索引 0 处获得名为“John”的对象,在索引 1 处获得名为“El”的对象

yes是的

BUT

The keys in the resulting object can be unordered, eg: it can be结果对象中的键可以是无序的,例如:它可以是

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

or或者

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

etc...等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM