Javascript 相当于 PHP 的 list()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1954426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript equivalent of PHP's list()
提问by Znarkus
Really like that function.
真的很喜欢那个功能。
$matches = array('12', 'watt');
list($value, $unit) = $matches;
Is there a Javascript equivalent of that?
是否有与此等效的 Javascript?
采纳答案by Nicolás
There is, in 'newer' versions of Javascript: Destructuring assignment - Javascript 1.7. It's probably only supported in Mozilla-based browsers, and maybe in Rhino.
在 Javascript 的“较新”版本中有:Destructuring assignment - Javascript 1.7。它可能只在基于 Mozilla 的浏览器中受支持,也可能在 Rhino 中受支持。
var a = 1;
var b = 3;
[a, b] = [b, a];
EDIT: actually it wouldn't surprise me if the V8 Javascript library (and thus Chrome) supports this. But don't count on it either Now supportedin all modern browsers(except IE, of course).
编辑: 实际上,如果 V8 Javascript 库(以及 Chrome)支持这一点,我不会感到惊讶。但也不要指望它现在所有现代浏览器都支持(当然,IE 除外)。
回答by The Mask
try this:
尝试这个:
matches = ['12', 'watt'];
[value, unit] = matches;
回答by Ben Fortune
ES6 does support this directly now via array destructuring.
ES6 现在通过数组解构直接支持这一点。
const matches = ['12', 'watt'];
const [value, unit] = matches;
回答by lac_dev
This is my solution for using List/Explode on Javascript. Fiddle Working Example
这是我在 Javascript 上使用 List/Explode 的解决方案。 小提琴工作示例
First the implementation :
首先实现:
var dateMonth = "04/15";
dateMonth.split("/").list("month","day", "year");
month == "04";
day == "15";
year == null;
It also allows for scoping the new generated 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";
})();
This was accomplished by modifying an the Array prototype.
这是通过修改 Array 原型来实现的。
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;
}
}
回答by 00dani
CoffeeScriptoffers destructuring assignment with the syntax:
CoffeeScript提供了具有以下语法的解构赋值:
[a, b] = someFunctionReturningAnArray()
This is pretty much identical to the feature offered in very new JavaScript versions. However, CoffeeScript produces compiled JS that is compatible even with IE6's JavaScript engine, and therefore it's a good option if compatibility is vital.
这与非常新的 JavaScript 版本中提供的功能几乎相同。但是,CoffeeScript 生成的编译 JS 甚至与 IE6 的 JavaScript 引擎兼容,因此如果兼容性至关重要,它是一个不错的选择。
回答by powtac
There is a experimental implementation of list()by PHPJShere:
https://github.com/kvz/phpjs/blob/master/_experimental/array/list.js
有一个实验性的实施list()由PHPJS这里:
https://github.com/kvz/phpjs/blob/master/_experimental/array/list.js
回答by rckd
Since most JavaScript implementations don't yet support that feature, you could simply do it in a more JavaScript-like fashion:
由于大多数 JavaScript 实现尚不支持该功能,您可以简单地以更类似于 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;
};
}
Example:
例子:
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"
An alternative is to add a list-method to Array.prototype (even I wouldn't recommend it):
另一种方法是向 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:
例子:
/**
* 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"
回答by i_a
This is my hack at it; as short as I could get it without writing a function to do it. Gotta be careful of the scope of "this" though:
这是我的技巧;尽可能短,而无需编写函数来完成它。不过要小心“这个”的范围:
list = ["a","b","c"];
vals = [1,2,3];
for(var i in vals)this[list[i]]=vals[i];
console.log(a,b,c);
Good enough for a laugh. I still assign each variable one at a time:
足以让人开怀大笑。我仍然一次为每个变量分配一个:
a=vals[0];
b=vals[1];
c=vals[2];
It's much shorter this way. Besides, if you've got a bunch of variables they should probably be kept in the array, or even better they should be properties of a closure, instead of declaring them all separately.
这种方式要短得多。此外,如果你有一堆变量,它们可能应该保存在数组中,或者更好的是它们应该是闭包的属性,而不是单独声明它们。
回答by EasyBB
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);
}
}
}
Example:
例子:
//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

