[英]Is it possible to have multiple keys point to the same object?
Basically I want to call myList[i]
and get the same value for i = 1, 2, 3, 4 then a second value for 5, 6, 7, 8 and so on.基本上我想调用myList[i]
并为 i = 1、2、3、4 获得相同的值,然后为myList[i]
等获得第二个值。 I don't really want to copy-paste the value for 1 into 2, 3, 4.我真的不想将 1 的值复制粘贴到 2、3、4 中。
You can try using this -你可以尝试使用这个 -
myList[Math.floor((i-1)/4)]
where i = 1, 2, 3, 4 ...
But if i
starts from 0
, then但是如果i
从0
开始,那么
myList[Math.floor(i/4)]
Adjust the +/- 1 according to your requirements.根据您的要求调整 +/- 1。
Where 4
is the block size to which you need to divide the array with.其中4
是您需要将数组划分为的块大小。
You can use an array of objects您可以使用一组对象
const arr = [...Array(4).fill({ value: 1 }), ...Array(4).fill({ value: 3 })]; console.log(arr[0].value); console.log(arr[1].value); console.log(arr[2].value); console.log(arr[3].value); console.log(arr[4].value); console.log(arr[5].value); console.log(arr[6].value); console.log(arr[7].value); arr[0].value = 2; console.log(arr[0].value); console.log(arr[1].value); console.log(arr[2].value); console.log(arr[3].value); console.log(arr[4].value); console.log(arr[5].value); console.log(arr[6].value); console.log(arr[7].value);
Each element is a reference to the same object.每个元素都是对同一个对象的引用。
That's not possible with an array of primitives.对于原始数组,这是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.