在我的 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/