JavaScript:将数组拆分为单个变量 [英] JavaScript: Split array into individual variables

查看:40
本文介绍了JavaScript:将数组拆分为单个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个数据结构:

var vehicles = [
  [ "2011","Honda","Accord" ],
  [ "2010","Honda","Accord" ],
  .....
];

循环遍历每个 vehicles 项,有没有办法一次性将数组元素重新分配给各个变量,例如:

Looping through each vehicles item, is there a way to reassign the array elements to individual variables all in one shot, something like:

for (i = 0; i < vehicles.length; i++) {
  var(year,make,model) = vehicles[i]; // doesn't work
  .....
}

...我正在努力摆脱:

... I'm trying to get away from doing:

for (i = 0; i < vehicles.length; i++) {
  var year = vehicles[i][0];
  var make = vehicles[i][1];
  var model = vehicles[i][2];
  .....
}

只是好奇,因为这种类型的东西可以在其他编程语言中使用.谢谢!

Just curious since this type of thing is available in other programming languages. Thanks!

推荐答案

不幸的是,目前 XBrowser 没有一种方法可以做到这一点.(我知道).

No unfortunately there is not a method to do this currently XBrowser. (that I'm aware of).

很快就可以跨浏览器了,见链接:

Relatively soon it's possible cross browser, see link:

https://developer.mozilla.org/en/New_in_JavaScript_1.7

(在 PHP 中有列表",它可以完全按照您的意愿行事,还没有类似的 XBrowser for javascript)

(In PHP there is "list" which will do exactly what you wish, nothing similar XBrowser for javascript yet)

当然,相对较快可能意味着什么等.(感谢 Felix 指出我在这方面的错误)

Of course relatively soon could mean anything etc. (Thanks Felix for pointing out my errors in this)

这篇关于JavaScript:将数组拆分为单个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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