Before, when writing wechat applet project , There is one in it “ City choice ” Function of , I use <picker-view> assembly , This component is special , Because it is value What are the attributes array
Formatted . such as :value="[1]".

Because I was right at that time JS Unknown variable type conversion , The author wrote such a few lines of judgment in the code :( This is rigorous )
let val_one=typeof this.data.pIndex=="number"?[this.daya.pIndex]:this.data.
pIndex
(: Access elements in a project are dynamic !

This is because the subscript should follow the user's selection dynamically , And feedback to wxml Inside value Display on attributes .
But before that, we need to make a judgment ——
Because some areas are provincial-level cities or municipalities directly under the central government , And to prevent users “ Sao operation ”, For example, pull up all the time or pull out violently , At this time, the wechat applet will report an error, and the corresponding data cannot be found :
let length=placeArray[val_one].sub.length if(val[0]>=length){ val=[length-1] }
else if(val[0]<0){ val=[0] }
Later, when I went back to optimizing the project code, I found that ( Convert arrays to numbers when forced , Turn numbers into arrays in feedback ) It's not necessary :

JavaScript It seems to have its own view of data “ unique ” How to deal with it ~~

How to judge whether a value can be used as an array subscript ( Indexes )

To be sure : Assigning values to integer property keys is a special case of arrays , Because they are not handled in the same way as non integer keys . To determine whether a property can be used as an index of an array , I found it ES6 A paragraph in the specification document :

Currently only if ToString(ToUint32(P)) be equal to P, also ToUint32(P) Not equal to 2^32-1 Time , String property name P It's an array index .

It's not very effective JS This can be done :
function toUint32(value){ return Math.floor(Math.abs(Number(value))) % Math.pow
(2,32); } function isArrayIndex(key){ let numericKey=toUint32(key); return
String(numericKey) == key && numericKey < (Math.pow(2,32)-1); }
* toUint32() The function converts a given value to an unsigned value through the algorithm described in the specification 32 Bit integer ;
* isArrayIndex() Function to convert the key to uint32 structure , Then compare them twice (toString() Is the last equal not equal to the original number and less than 2^32-1 )

With this foundation , We can simply imitate it based on this new Array() act —— The most important one is about length Description of :

function createArray(length=0){ return new Proxy({ length },{ set(trapTarget,
key,value){ let currentLength=Reflect.get(trapTarget,"length"); if(isArrayIndex(
key)){ let numericKey=Number(key); if(numericKey >= currentLength){ Reflect.set(
trapTarget,"length",numericKey+1); } }else if(key === "length"){ if(value <
currentLength){ for(let index=currentLength-1;index >= value;index--){ Reflect.
deleteProperty(trapTarget,index); } } } // whether key This code is executed for all types return Reflect.set
(trapTarget,key,value); } }); }
Experiment :

Technology
©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree