0

Does JavaScript have a language construct or something similar to the php list command? http://php.net/manual/en/function.list.php

This command will assign the values of an array to variables in a single statement. For example, given the array:

$info = array('Coffee', 'brown', 'caffeine');

The list command will assign each of the array element values to named variables:

list($drink, $color, $power) = $info;

such that:

   echo "$drink is $color and $power makes it special."; // results in:

   Coffee is brown and caffeine makes it special.

So this is an easy way to assign values to many variables in one statement.

Does JavaScript have something equivalent where each variable does not have to be assigned separately?

Is there a way to do this using either an object's properties or an array's elements? If not, can a function be written that would do it? If the only way to do this is via a function, then the function would need access to the scope where the variables are defined.

I tried the solutions in this five year old question: Javascript equivalent of PHP's list(), but they don't work. The Array prototype change fails to assign the variables in my node.js environment and the left handed array assignment is a reference error in Chrome. There is talk about an experimental new technique, but the talk is several years old. I'd like to know if there is a solution to this better than those listed in the linked question.

Community
  • 1
  • 1
ciso
  • 2,887
  • 6
  • 33
  • 58
  • Of course a function can be written to do this, but why not skip the interim steps and simply directly create an object with named properties? It's a while since I've really worked with PHP, but I can't recall ever having a use-case for this scenario. – David Thomas Feb 19 '15 at 21:45
  • Though this looks like a duplicate to this, it would be interesting to see if there is a new answer to this, as the other question is more than five years old. http://stackoverflow.com/questions/1954426/javascript-equivalent-of-phps-list – baao Feb 19 '15 at 21:48
  • @DavidThomas is the answer to the other question still correct? Please see the questions date. – baao Feb 19 '15 at 21:50
  • @michael: I saw, but I think it's still applicable. If Chris tries to use those answers and finds they don't work then he can always edit this question to address the fact that the answers in the linked question aren't applicable (ideally explaining how they fail). That edit should get it into the reopen queue, and if he notifies me with an `@comment` I can always undo my close-as-duplicate vote. – David Thomas Feb 19 '15 at 21:53
  • @DavidThomas true, I was wondering if I should vote to close and only didn't because of the date, but your handling seems better – baao Feb 19 '15 at 21:56
  • 1
    I'm sorry to hear that, Chris; I'll reopen the question momentarily, but please [edit] your question to add that information ("I tried the answers from this question [link], they didn't work because...") to prevent someone else from closing as a dupe again, once your question's reopened. Best of luck! – David Thomas Feb 20 '15 at 00:07
  • 1
    @DavidThomas: You're lucky I read through the comments, I nearly had hammered the question just again :-) Destructuring is still state of the art, so this question needs to add a lot new things. Maybe a bounty on the other question would be more appropriate. – Bergi Feb 20 '15 at 00:12
  • 1
    I'm not as comfortable posting it as an answer (since I'm not an expert in node.js), but it appears that node does not yet have destructuring: http://stackoverflow.com/q/17379277/899126 – Chris Forrence Feb 20 '15 at 00:14
  • 1
    I think node (v8) will implement this in the near future, given that it's in ES6. Until then, you're left with manual assignments (or a cross-compiler). – Bergi Feb 20 '15 at 00:19

4 Answers4

3

I've just written a simply function, and it works. Just not exactly like list in php. Here it is

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);
       }
   }
}

It's nothing spectacular but if you are looking for something simple and use case for yourself there it is.

How to use this?

//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

Hope that's what you were looking for

EasyBB
  • 6,176
  • 9
  • 47
  • 77
3

This is called "destructuring assignment" in ECMAScript 6. Mozilla Developer Network has some examples:

var a, b;
[a, b] = [1, 2]
{a, b} = {a:1, b:2}

It is supported in Node.js since at least version 8, and also in some browsers, as you can see in this ECMAScript compatibility table. You can still use it if you transpile your code to ECMAScript 5 with something like Babel.

Lars Nyström
  • 5,786
  • 3
  • 32
  • 33
1

There's a experimental function written by php.js that tries to adapt php's list function to js, maybe you can built on this:

function list() {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %        note 1: Only works in global context and deviates (by necessity) from
  // %        note 1: PHP version by adding the array (which in PHP is an rvalue
  // %        note 1: separate from the list() lvalue) as the last argument
  // *     example 1: var drink, color, power;
  // *     example 1: list('drink', 'color', 'power', ['coffee', 'brown', 'caffeine']);
  // *     example 1: drink +' is '+color+' and '+power+' makes it special.\n';
  // *     returns 1: 'coffee is brown and caffeine makes it special.\n'

  var i = 0, arr = [];

  arr = arguments[arguments.length - 1];

  if (arr && typeof arr === 'object' && arr.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
    return arr.list.apply(arr, Array.prototype.slice.call(arguments, 0, -1));
  }
  if (arr && typeof arr === 'object' && arr.length && !arr.propertyIsEnumerable('length')) {
    for (i = 0; i < arr.length; i++) {
      this.window[arguments[i]] = arr[i];
    }
  }
  else {
    for (i in arr) {
      if (i.length === parseInt(i).toString().length && parseInt(i) < arguments.length - 1) {
        this.window[arguments[i]] = arr[i];
      }
    }
  }

  return arr;
}
baao
  • 71,625
  • 17
  • 143
  • 203
1

I think the accepted answer by EasyBB could be simplified greatly for cases where you don't have nested arrays.

var info = [ "Coffee", "brown", "caffeine" ];

(function(drink, color, power) {
    console.log(drink + " is " + color + " and " + power + " makes it special.");
}.apply(this, info));

Now you don't have to define an extra function to get the job done. Of course, it will be much nicer when ES6 is adopted and we can use array destructuring. In the meantime, I would probably just use the old fashioned var drink = info[0], color = info[1], power = info[2];; it's more readable and uses less memory.

Community
  • 1
  • 1
Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45