Tutorial :searching equivalent function/way to “list()” from php



Question:

i'm searching for an equivalent function for php's "list()". i'd like to split a string with "split()" and put the array values in two different variables. hopefully there's another way to do this as short and performance-optimized as it could be.

thanks for your attention!


Solution:1

Here's a list implementation in javascript:

http://solutoire.com/2007/10/29/javascript-left-hand-assignment/


Solution:2

Maybe PHP.JS is worth looking at (for future stuff maybe). I noticed that the list() was only experimental though but maybe it works.


Solution:3

If you can live without the naming, you can do this:

var foo = ["coffee", "brown", "caffeine"];  foo[0] + " is " + foo[1] + " and  " + foo[2] + " makes it special";  

Alternatively, use a object to name the keys:

var foo = {drink: "coffee", color: "brown", power: "caffeine"};  foo.drink + " is " + foo.color + " and " + foo.power + " makes it special";  


Solution:4

I think the closest you can get is the explicit

var args = inputString.split(",");    // Error checking, may not be necessary  if (args.length < 3)  {     throw new Error("Not enough arguments supplied in comma-separated input string");  }    var drink = args[0];  var color = args[1];  var power = args[2];  

since Javascript doesn't have multiple assigment operators. I wouldn't worry too much about the efficiency of this; I expect that the list function in PHP basically boils down to the same thing as above, and is just syntactic sugar. In any case, unless you're assigning hundreds of thousands of variables, the time to execute anything like the above is likely to be negligible.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »