遍历对象的顺序可能仅在迭代期间被破坏?

2024-04-18前端开发问题
2

本文介绍了遍历对象的顺序可能仅在迭代期间被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我猜循环对象的首选方式是这样的:

I guess the preferred way of looping through an object is this:

for (var prop in obj) {
  if( obj.hasOwnProperty( prop ) ) {
    console.log("obj." + prop + " = " + obj[prop]);
  } 
}

MDN 是这么说的

已删除、添加或修改的属性for...in 循环以 任意顺序 迭代对象的属性(请参阅 delete 运算符了解更多关于为什么不能依赖迭代的表面顺序,至少在交叉浏览器设置).

Deleted, added or modified properties A for...in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting).

因此,如果我在迭代期间不修改对象属性,我可以保证正确的顺序,即键/属性出现在对象中的顺序,或者此语句意味着其他什么?

So if I don't modify object properties during iteration I can be guaranteed the correct order i.e. the order in which keys/properties appear in an object or this statement means something else?

推荐答案

MDN 页面删除 声明:

...所有主要浏览器都支持基于最早添加的属性的迭代顺序...但是,在 Internet Explorer 的情况下,当对属性使用 delete [并] 添加回具有相同属性的属性时名称,属性将在其旧位置迭代——而不是在迭代序列的末尾......

...all major browsers support an iteration order based on the earliest added property coming first... However, in the case of Internet Explorer, when one uses delete on a property [and] adds back a property with the same name, the property will be iterated in its old position -- not at the end of the iteration sequence...

插图:

var obj = {};

obj.x = 1; 
obj.y = 2;
obj.z = 3;

var a = [];
for(var i in obj) a.push(i);

delete obj.y;
obj.y = 2;

var b = [];
for(var i in obj) b.push(i);


document.write("1:" + a + "<br>2:" + b);

Chrome/FF/Safari 显示 1:x,y,z 2:x,z,y,而在 MSIE(和 Edge)中,结果是 1:x,y,z 2:x,y,z.

Chrome/FF/Safari display 1:x,y,z 2:x,z,y, while in MSIE (and Edge) the result is 1:x,y,z 2:x,y,z.

请注意,与 ES5 不同,ES6 要求属性必须按创建顺序迭代:

Note that unlike ES5, ES6 mandates that the properties must be iterated in the creation order:

对于O的每个自己的属性键P,它是一个字符串但不是整数索引,按属性创建顺序...

For each own property key P of O that is a String but is not an integer index, in property creation order...

http://www.ecma-international.org/ecma-262/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys

标准不是很清楚创建顺序"到底是什么意思.MS 认为重要的是 initial 创建时间,而其他人使用 last 创建时间.

The standard is not very clear what exactly "creation order" means. MS takes it that it's the initial creation time that matters, while others use the last creation time.

这篇关于遍历对象的顺序可能仅在迭代期间被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125