2009-12-23 39 views
50

真的很喜歡那個功能。Javascript的PHP列表()

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

是否有與此相當的Javascript?

+0

標準方法有什麼問題var value = matches [0]; var unit = matches [1]; ' – 2009-12-23 18:17:50

+7

那麼,這不是很簡潔,是嗎? – Kzqai 2009-12-23 18:18:04

+1

我從來沒有覺得'list()'是有用的,上面只是大聲反對我'var power = {'unit':'watt','amount':12}' – Gordon 2009-12-23 18:29:51

回答

37

有,但在「新」版本的Javascript:Destructuring assignment - Javascript 1.7。它可能只支持基於Mozilla的瀏覽器,也可能支持Rhino。

var a = 1; 
var b = 3; 

[a, b] = [b, a]; 

編輯:實際上,如果V8 Javascript庫(以及Chrome)支持這個功能,我不會感到驚訝。但不要指望它要麼:)

+6

整潔!我真的很喜歡他們已經加入新版Javascript的所有酷東西!只是覺得我們將無法使用它們的年齡.. – Znarkus 2009-12-23 18:38:17

+0

代碼在哪裏? – 2011-05-28 02:45:39

+1

@David https://developer.mozilla.org/en/New_in_JavaScript_1.7#section_20 – Znarkus 2011-06-30 18:20:59

17

試試這個:

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

適用於FF4,但不適用於Chrome 12.無論如何,酷! – Znarkus 2011-06-16 06:05:57

2

CoffeeScript報價解構與語法分配:

[a, b] = someFunctionReturningAnArray() 

這幾乎是與非常新的JavaScript版本中提供的功能相同。但是,CoffeeScript生成的編譯JS與IE6的JavaScript引擎兼容,因此如果兼容性至關重要,這是一個不錯的選擇。

4

這是我在Javascript上使用List/Explode的解決方案。 Fiddle Working Example

首先實施:

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

它還允許範圍界定新生成的變量:

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; 
    } 
} 
+0

我堅持我的解決方案。如果你嘗試類似: matches = ['12','watt']; [value,unit] =匹配; 或 function(){ var [year,month] = $(this).val()。分裂(」/」); 在Chrome中,它會拋出一個錯誤: 「ReferenceError:賦值中無效的左側」 – 2013-07-24 18:51:54

+0

使用'window'作爲默認對象是一個非常糟糕的主意。 – Bergi 2015-02-20 00:17:34

+0

@Bergi他只默認它。你可以提供一個對象作爲最後一個參數,它將使用它。 – micahblu 2015-12-10 20:10:38

0

這是我的破解;儘可能短,而不用寫一個函數來完成它。但要注意「這個」的範圍,但:

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

好夠笑了。我仍然一次給每個變量分配一個:

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

這樣的方式很短。此外,如果你有一堆變量,它們應該保存在數組中,或者甚至更好,它們應該是閉包的屬性,而不是單獨聲明它們。

-2
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 
1

由於大多數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; 
    }; 
} 

例子:

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" 

Check the fiddle


另一種方法是添加一個列表-方法應用到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" 

Check the fiddle for that one

6

ES6現在通過array destructuring直接支持。

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