gpt4 book ai didi

javascript - 当键名具有数值时,JSON.parse() 是否真的对属性进行排序?

转载 作者:行者123 更新时间:2023-11-29 10:33:55 24 4
gpt4 key购买 nike

这里有很多关于这个问题的帖子,它们都包含很多断言,可以总结如下:

  1. 从不保证以任何方式对对象属性进行排序。
  2. JSON.parse() 从不以任何方式对属性进行排序。

显然,我们倾向于对上面的第 1 点毫无疑问,因此我们可以合理地预期,对于任何操作,属性仅按照它们出现的顺序进行处理。
[根据@Bergi 的评论进行编辑:或者至少它们应该以随机顺序出现]

然后我们可能会特别推断出#2 应该是真的。

但是看看这个片段:
(顺便说一句:为了显示结果,下面的代码片段不使用 console.log(),它本身可能会更改输出的顺序。相反,对象由 for (key in obj) 和文档中显示的输出)

var inputs = [
'{"c": "C", "a": "A", "b": "B"}',
'{"3": "C", "1": "A", "2": "B"}',
'{"c": "C", "a": "A", "3": "C", "1": "A", "b": "B", "2": "B"}'
];

for (var i in inputs) {
var json = inputs[i],
parsed = JSON.parse(json),
output = [];
for (var j in parsed) {
output.push(j + ': ' + parsed[j]);
}
document.write(`JSON: ${json}<br />Parsed: ${output.join(', ')})<hr />`);
}

它表明,给定一个具有无序键的 JSON 字符串:

  • 当输入包含具有非数字值的键时,解析对象的属性属性顺序与输入中的相同。这与上面的 #2 假设一致。
  • 相反,当输入有带数值的键时(虽然它们是字符串,所以不会引发解析错误),解析对象的属性已排序。这现在与 #2 假设相矛盾。
  • 更多:当存在混合数字和非数字键值时,首先出现已排序的数字属性,然后是原始的非数字属性订购

据此,我首先想得出结论,实际上会有一个(未记录?)功能,因此 JSON.parse() 遵循上面公开的“规则”工作。

但我想更进一步,所以下面的代码片段现在显示了一个仅编码对象的属性是如何排序的:

var objects = [
[
'{"c": "C", "a": "A", "b": "B"}',
{"c": "C", "a": "A", "b": "B"}
],
[
'{"3": "C", "1": "A", "2": "B"}',
{"3": "C", "1": "A", "2": "B"}
],
[
'{"c": "C", "a": "A", "3": "C", "1": "A", "b": "B", "2": "B"}',
{"c": "C", "a": "A", "3": "C", "1": "A", "b": "B", "2": "B"}
]
];

for (var i in objects) {
var object = objects[i],
output = [];
for (var j in object[1]) {
output.push(j + ': ' + object[1][j]);
}
document.write(`Code: ${object[0]}<br />Object: ${output.join(', ')}<hr />`);
}

它导致模拟观察,即无论它们的编码顺序如何,属性都按照上面的第三条规则存储:

  • 以数字命名的属性都放在最前面,并排序
  • 接下来设置其他属性,按编码排序

所以这意味着不涉及JSON.parse():实际上它似乎是对象构建的基本过程。
同样,这似乎没有记录,至少据我所知。

任何关于真实、权威规则的线索?


[编辑,感谢@Oriol 的回答] 实际上,综合来看:

  • 此行为符合 ECMA specification rule .
  • 此规则应适用于保证特定顺序的所有方法但在其他情况下是可选的
  • 然而,似乎现代浏览器都选择无论涉及何种方法都应用该规则,因此存在明显的矛盾。

最佳答案

对象的属性没有顺序,所以 JSON.parse 无法对它们进行排序。但是,当您列出或枚举对象的属性时,顺序可能定义明确或不明确。

不一定用于 for...in 循环或 Object.keys

详见 Does ES6 introduce a well-defined order of enumeration for object properties? , spec

The mechanics and order of enumerating the properties is not specified

但是对于 OrdinaryOwnPropertyKeys 是的

对象有一个内部的 [[OwnPropertyKeys]] 方法,它被 Object.getOwnPropertyNamesObject.getOwnPropertySymbols 使用。

对于普通对象,该方法使用 OrdinaryGetOwnProperty抽象操作,以明确定义的顺序返回属性:

When the abstract operation OrdinaryOwnPropertyKeys is called with Object O, the following steps are taken:

  1. Let keys be a new empty List.
  2. For each own property key P of O that is an integer index, in ascending numeric index order
    1. Add P as the last element of keys.
  3. For each own property key P of O that is a String but is not an integer index, in ascending chronological order of property creation
    1. Add P as the last element of keys.
  4. For each own property key P of O that is a Symbol, in ascending chronological order of property creation
    1. Add P as the last element of keys.
  5. Return keys.

因此,由于 OrdinaryOwnPropertyKeys 需要一个顺序,因此实现可能决定按该顺序在内部存储属性,并在枚举时也使用它。这是您观察到的,但您不能依赖它。

还要注意非普通对象(例如代理对象)可能有另一个 [[OwnPropertyKeys]] 内部方法,所以即使在使用 Object.getOwnPropertyNames 时顺序仍然可能不同。

关于javascript - 当键名具有数值时,JSON.parse() 是否真的对属性进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39985045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com