如何在 JavaScript 中创建一个索引从 1 开始的数组? [英] How to create an array in JavaScript whose indexing starts at 1?
问题描述
默认情况下,每个 JavaScript 数组的索引从 0 开始.我想创建一个索引从 1 开始的数组.
By default the indexing of every JavaScript array starts from 0. I want to create an array whose indexing starts from 1 instead.
我知道,一定很琐碎...谢谢你的帮助.
I know, must be very trivial... Thanks for your help.
推荐答案
这不是微不足道的.不可能.您能做的最好的事情是使用从 1 开始的数字属性创建一个对象,但这不是一回事.
It isn't trivial. It's impossible. The best you could do is create an object using numeric properties starting at 1 but that's not the same thing.
为什么你希望它从 1 开始?要么:
Why exactly do you want it to start at 1? Either:
从 0 开始并根据需要调整索引;或
Start at 0 and adjust your indices as necessary; or
从 0 开始,忽略索引 0(即只使用索引 1 及以上).
Start at 0 and just ignore index 0 (ie only use indices 1 and up).
这篇关于如何在 JavaScript 中创建一个索引从 1 开始的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!