3
The other day debugging a code here on the page I found something peculiar.
Like that:
let a = {};
let b = [1,2,3];
let c = [4,2,3];
a[b]= 1;
a[c]= 2;
console.log(a) //{ "1,2,3": 1, "4,2,3": 2 } <--- Preste atenção
In other words, the author of the question had passed a array as a property advisor using the bracket notation and could no longer locate the property.
This, however, showed me an interesting behavior of the language. An array, example ["1,2,3"]
, when passed as a property assessor using bracket notation with creates a property whose identifier is a string, in the example the string "1,2,3"
.
Using this behavior I created this code:
const regSlice = /^\d+(,\d+){0,2}$/;
function proxiedArray(array) {
return new Proxy(array, {
get: (obj, key) => {
if (typeof key == "string") {
if (regSlice.test(key)) {
let [start, stop, step] = key.split(',').map((v) => parseInt(v));
stop = (stop != undefined) ? stop : start + 1;
step = (step != undefined) ? step : 1;
let c = step - 1;
return obj.slice(start, stop).filter((e, i) => ((c < step) ? c++ : c = 1, c % step == 0));
}
}
return obj[key];
}
});
}
const a = proxiedArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
console.log(a[[0, 9, 2]]); //[ 1, 3, 5, 7, 9]
Code that I tested in some Engins(Spidermonkey, V8) getting the same result achieved here.
The above code defines a function proxedArray(array)
involving an object array in a Proxy giving the array a peculiar property, the power of being sliced by index such as a python list:
identificador_do_array"[" fatia "]"
fatia = array"[" inicio[, fim[, passo]] "]"
Where:
inicio
is the index where the.fim
is the index where the slice will end. Optional.passo
is the variation between the indices of the elements to be considered successive. Optional. Missing is 1.
So before I further develop this code, and put it into production environment, I need to ask the following question::
There is documentation in the Ecmascript standard to ensure that an array when passed as a property advisor has a defined format?
I didn’t find anything about it, not saying yes or no.
That, is that one the proof you were looking for and couldn’t find. Thank you!!!!
– Augusto Vasques