Javascript等价于PHP的列表()

真的很喜欢那个function。

$matches = array('12', 'watt'); list($value, $unit) = $matches; 

有没有一个Javascript的相当于?

有,但在“新”版本的Javascript: 解构赋值 – JavaScript 1.7 。 它可能只支持基于Mozilla的浏览器,也可能支持Rhino。

 var a = 1; var b = 3; [a, b] = [b, a]; 

编辑:实际上它不会让我感到惊讶,如果V8 JavaScript库(从而铬)支持这一点。 但不要指望它:)

尝试这个:

 matches = ['12', 'watt']; [value, unit] = matches; 

ES6现在通过数组解构来直接支持这个。

 const matches = ['12', 'watt']; const [value, unit] = matches; 

这是我在Javascript上使用List / Explode的解决scheme。 小提琴工作例子

首先执行:

 var dateMonth = "04/15"; dateMonth.split("/").list("month","day", "year"); month == "04"; day == "15"; year == null; 

它也允许范围新的生成variables:

 var scoped = (function() { var dateMonth = "07/24/2013"; dateMonth.split("/").list("month","day", "year", this); this.month == "07"; this.day == "24"; this.year == "2013"; })(); 

这是通过修改数组原型来完成的。

 Array.prototype.list = function() { var limit = this.length, orphans = arguments.length - limit, scope = orphans > 0 && typeof(arguments[arguments.length-1]) != "string" ? arguments[arguments.length-1] : window ; while(limit--) scope[arguments[limit]] = this[limit]; if(scope != window) orphans--; if(orphans > 0) { orphans += this.length; while(orphans-- > this.length) scope[arguments[orphans]] = null; } } 

这里有一个由PHPJS实现的list()的实现 :
https://github.com/kvz/phpjs/blob/master/_experimental/array/list.js

CoffeeScript提供解构赋值语法:

 [a, b] = someFunctionReturningAnArray() 

这与非常新的JavaScript版本中提供的function非常相似。 但是,CoffeeScript生成的编译JS与IE6的JavaScript引擎兼容,因此如果兼容性至关重要,那么这是一个不错的select。

由于大多数JavaScript实现还不支持该function,因此您可以简单地以更类似于JavaScript的方式进行操作:

 function list(){ var args = arguments; return function(array){ var obj = {}; for(i=0; i<args.length; i++){ obj[args[i]] = array[i]; } return obj; }; } 

例:

 var array = ['GET', '/users', 'UserController']; var obj = {}; obj = list('method', 'route', 'controller')(array); console.log(obj.method); // "GET" console.log(obj.route); // "/users" console.log(obj.controller); // "UserController" 

检查小提琴


另一种方法是将一个列表方法添加到Array.prototype(即使我不推荐它):

 Array.prototype.list = function(){ var i, obj = {}; for(i=0; i<arguments.length; i++){ obj[arguments[i]] = this[i]; } // if you do this, you pass to the dark side `,:,´ this.props = obj; return obj; }; 

例:

 /** * Example 1: use Array.prototype.props */ var array = ['GET', '/users', 'UserController']; array.list('method', 'route', 'controller'); console.log(array.props.method); // "GET" console.log(array.props.route); // "/users" console.log(array.props.controller); // "UserController" /** * Example 2: use the return value */ var array = ['GET', '/users', 'UserController']; var props = array.list('method', 'route', 'controller'); console.log(props.method); // "GET" console.log(props.route); // "/users" console.log(props.controller); // "UserController" 

检查那个小提琴

这是我的黑客攻击; 尽可能短,而不用写一个函数来做到这一点。 但要注意“这个”的范围:

 list = ["a","b","c"]; vals = [1,2,3]; for(var i in vals)this[list[i]]=vals[i]; console.log(a,b,c); 

足够好笑了。 我仍然每次分配一个variables:

 a=vals[0]; b=vals[1]; c=vals[2]; 

这种方法要短得多。 此外,如果你有一堆variables,他们应该保存在数组中,甚至更好的是它们应该是闭包的属性,而不是单独声明它们。

 function list(fn,array){ if(fn.length && array.length){ for(var i=0;i<array.length;i++){ var applyArray = []; for(var j=0;j<array[i].length;j++){ fn[j] = array[i][j]; applyArray.push(fn[j]); } fn.apply(this,applyArray); } } } 

例:

 //array array mixture for composure var arrayMixture = [ ["coffee","sugar","milk"], ["tea","sugar","honey"] ]; //call our function list(function(treat,addin,addin2){ console.log("I like "+treat+" with " + addin + " and " + addin2); },arrayMixture); //output: //I like coffee with sugar and milk //I like tea with sugar and honey