Data type problem in JavaScript array index detection

In writing letters before small micro projects program, which has a "City Select" function, I use the <picker-view>component, this component is rather special, because its valueproperties are prescribed an array format. For value="[1]"example: .

Because I didn’t understand the JS variable type conversion at the time, the author wrote the following 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 the project are dynamic!

This is because to make the above subscripts follow the user to select dynamically, and fed back to the wxml in valuethe attribute display.
But before that, we still need to make a layer of judgment-because some areas are provincial-level cities or municipalities, and users must prevent users from "spoofing", such as pulling up or pulling out suddenly. At this time, it is in the WeChat applet. An error will be reported if 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 optimize the code of this project, I found that this (turn the array into a number when forced to use, and turn the number into an array when feedback) is actually unnecessary:
detail

JavaScript seems to have its own "unique" way of processing data, but the author has not found relevant information yet~~


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

But what is certain is: Assigning integer attribute keys is a special case of arrays, because they are handled differently from non-integer keys. To determine whether a property can be used as an index of an array, I found a passage in the ES6 specification document:

Only when the current ToString(ToUint32(P))is equal to P, and ToUint32(P)not equal to 2^32-1, the name of the property P is a string array index.

This operation can be achieved with JS:

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);
}
  1. toUint32() The function converts the given value into an unsigned 32-bit integer through the algorithm described in the specification;
  2. isArrayIndex()In the function, the key is first converted to a uint32 structure, and then two comparisons are performed (the toString()latter is not equal to the original number and is less than 2^32-1)

With this foundation, we can go based on this simple imitate the new Array()behavior - the most important thing about the length of the description:
array_length

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);
					}
				}
			}
			// 无论key是什么类型都要执行这段代码
			return Reflect.set(trapTarget,key,value);
		}
	});
}

Experiment:
myArray_test

Guess you like

Origin blog.csdn.net/qq_43624878/article/details/112388692