将 json 数组转换为 javascript 数组
我有一个 json 数组,我想将其转换为纯 javascript 数组:
这是我的 json 数组:
var users = {"0":"John","1":"Simon","2":"Randy"}
如何将其转换为纯 javascript 数组,如下所示:
var users = ["John", "Simon", "Randy"]
i have a json array that i want to convert into a plain javascript array:
This is my json array:
var users = {"0":"John","1":"Simon","2":"Randy"}
How to convert it into a plain javascript array like this:
var users = ["John", "Simon", "Randy"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
users
已经是一个 JS 对象(不是 JSON)。但在这里你可以:编辑:在数组中的正确位置插入元素。感谢@RoToRa。
也许一开始就不创建这种对象会更容易。它是如何创建的?
users
is already a JS object (not JSON). But here you go:Edit: Insert elements at correct position in array. Thanks @RoToRa.
Maybe it is easier to not create this kind of object in the first place. How is it created?
只是为了好玩 - 如果您知道数组的长度,那么以下内容将起作用(并且似乎更快):
Just for fun - if you know the length of the array, then the following will work (and seems to be faster):
好吧,这里有一个 Jquery+Javascript 解决方案,对于那些感兴趣的人:
Well, here is a Jquery+Javascript solution, for those who are interested: