JavaScript对象存储顺序 [英] Javascript Object Storage Order

查看:52
本文介绍了JavaScript对象存储顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组

  let setOne = ["]]; 

现在,我创建了一个函数,该函数从数组中获取元素,即 setOne ,将该元素存储在一个 Object 中作为属性名称,并给该属性名称指定一个值 true 并打印对象数据.

功能是

 功能checkTheSameBetter(setOne){让ObjectShop = {};for(让indexSetOne = 0; indexSetOne< setOne.length; indexSetOne ++){ObjectShop [setOne [indexSetOne]] = true;}console.log(ObjectShop);} 

我得到的输出是

  {'':true} 

到目前为止没有问题

这是主要部分当我向数组中添加另一个值(即 setOne )时,请考虑"1".

  let setOne = [",1]; 

然后执行功能 checkTheSameBetter .我得到输出

  {'1':true,'':true} 

我的问题是,'1'是如何存储在第一个位置?"

我期望的输出按此顺序

  {'':true,'1':true} 

这是沙箱链接 https://codesandbox.io/s/cranky-cori-qx116?file =/src/index.js

有人可以告诉我这里发生了什么吗?如果您需要任何澄清,我会给出.

谢谢

解决方案

ES6定义了枚举对象自身属性的顺序.以下是根据枚举对象自身属性的规则:

  • 名称为非负数的字符串属性从小到大依次列出.这意味着将按顺序枚举数组和类似数组的对象的属性.

  • 在那之后,所有带有字符串名称的属性都按照它们在对象中添加的顺序列出.这还包括看起来像非负数或浮点数的属性.

  • 最后,以它们在对象中添加的顺序列出了名称为Symbols的属性.

以下功能按其自身的约束按上述顺序列出属性.

  • Object.keys()
  • Object.getOwnPropertyNames()
  • Object.getOwnPropertySymbols()
  • Reflect.ownKeys()

要记住的一件事是,对于 for in 循环的枚举顺序没有像上述枚举函数那样严格地指定,但是通常按上述顺序枚举自己的属性./p>

for in 循环还枚举了原型链中的属性,一旦枚举了自己的属性,它将随后向上移动原型链,以与所述顺序相同的顺序枚举每个原型对象的属性.以上.尽管如果已枚举某个属性,则不会再次枚举具有相同名称的任何属性.即使已经考虑了同名的不可枚举属性,也不会枚举该属性.

  const obj = {};obj [2] = 2;obj ['-1'] = -1;obj ['1'] = 1;obj ['as'] ='as';obj ['10'] = 10;obj ['b'] ='b';console.log(Reflect.ownKeys(obj));console.log(Object.getOwnPropertyNames(obj));for(obj中的const键){console.log(key);}  

  .as-console-wrapper {max-height:100%!important;最高:0;}  

This is my Array

let setOne = [""];

Now, I created a function that takes the element from the array i.e setOne, store that element in one Object as a property name and give that property-name a value of true and prints the object data.

Function is

function checkTheSameBetter(setOne) {
  let ObjectShop = {};
  for (let indexSetOne = 0; indexSetOne < setOne.length; indexSetOne++) {
    ObjectShop[setOne[indexSetOne]] = true;
  }
  console.log(ObjectShop);
}

The Output which I get is

{ '': true }

No Problem so far

Here comes the main part when I add another value to an array i.e setOne, consider "1".

let setOne = ["",1];

And then when I execute the function checkTheSameBetter. I get output

 {'1': true, '': true }

So my question is, "how did that '1' get stored in the first position?"

The output I expected was in this order

{'': true, '1': true}

Here is the sandbox Link https://codesandbox.io/s/cranky-cori-qx116?file=/src/index.js

can anyone please tell me what's happening here? If you need any clarification I will give it.

Thank You

解决方案

ES6 defines an order in which own properties of an object are enumerated. Following are the rules according to which own properties of an object are enumerated:

  • String properties whose names are non-negative numbers are listed first, from smallest to largest. This means that properties of array and array-like objects will be enumerated in order.

  • After that, all properties with string names are listed in the order they were added in the object. This also includes properties that look like non-negative numbers or floating point numbers.

  • At last, properties whose names are Symbols are listed in the order they were added in the object.

Following functions list the properties in the above described order, subject to their own constraints.

  • Object.keys()
  • Object.getOwnPropertyNames()
  • Object.getOwnPropertySymbols()
  • Reflect.ownKeys()

One thing to keep in mind is that enumeration order for for in loop is not as tightly specified as it is for above mentioned enumeration functions but it typically enumerates own properties in the order described above.

As for in loop also enumerates properties in the prototype chain, once own properties have been enumerated, it will then move up the prototype chain, enumerating properties of each prototype object in the same order as described above. Although if a property has already been enumerated, any property with the same name won't be enumerated again. Property won't be enumerated even if a non-enumerable property with the same name has already been considered.

const obj = {};

obj[2] = 2;
obj['-1'] = -1;
obj['1'] = 1;
obj['as'] = 'as';
obj['10'] = 10;
obj['b'] = 'b';

console.log(Reflect.ownKeys(obj));
console.log(Object.getOwnPropertyNames(obj));

for (const key in obj) {
  console.log(key);
}

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于JavaScript对象存储顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆