Home  >  Article  >  Web Front-end  >  How to solve the problem that the attribute is a non-negative integer when for in object in Vue

How to solve the problem that the attribute is a non-negative integer when for in object in Vue

不言
不言Original
2018-07-14 15:35:351956browse

This article mainly introduces how to solve the problem of non-negative integers when the for in object in Vue. It has a certain reference value. Now I share it with you. Friends in need can refer to it

When I make a requirement, for in an object, the attributes of the object are all numbers

But I want to add a default attribute and value to this object

The original object is {5446:" Guangzhou City"}. . . . . Similar to going on

Then I want to display a default value on my page which is "Select City"

Then I put {"":"Select City"} at the front of the object Added

but the reality is not as I thought. The object is cycled and the selected city is at the top instead of at the bottom. But I can’t figure it out. Obviously my object is {"":"Select the city ",5446:"Guangzhou City"......}Like this

Why is the selected city still at the bottom of the list? Then I went to check the information

and saw vue The official website says that for in is sorted according to Object.keys

Object.keys is the same as for...in (I don’t know if there is a problem with this sentence)

Then I Demoed it on the console

var a={"":"123",name:"wenwen"}
for(var i in a){console.log(i)}
"" 
name

This is displayed normally

But then something strange happened, what happens when the attribute is a non-negative integer

var a={"":"123",123:"这是一个数字123",12:"这是一个数字12"}


for(var i in a){console.log(i)}
 123
 ""

Did you find the problem? Things are not as I thought "" 123 12 Print like this

Then I checked the information and found out

They will extract all keys first parseFloat attributes whose value is a non-negative integer, then sort the attributes according to numerical order and traverse them first, and then traverse all remaining attributes in the order defined by the object. Other browsers traverse properties exactly in the order in which the object is defined.

The attribute traversal order description in the new version is different from the earlier version, which will cause the JavaScript parsing engine implemented following the content of the ECMA-262 third edition specification to process When using a for-in statement, there is an inconsistency in the order of attribute traversal with the parsing engine implemented in compliance with the fifth edition specification

The above is the entire content of this article, I hope it will be helpful to everyone’s learning Helpful, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Implementation of mobile web music player based on vue

How to use native js to implement Ajax

The above is the detailed content of How to solve the problem that the attribute is a non-negative integer when for in object in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn