如何在JavaScript中创建索引从1开始的数组? [英] How to create an array in JavaScript whose indexing starts at 1?

查看:538
本文介绍了如何在JavaScript中创建索引从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆