Hello everyone make sure to stop by and introduce yourself and do check out our design and programming sections.
Visit the Den and get some free stuff!

You are not connected. Please login or register

JavaScript equivalent of PHPs list function

View previous topic View next topic Go down  Message [Page 1 of 1]

1 JavaScript equivalent of PHPs list function on Thu Feb 19, 2015 10:19 pm

TheDeveloper

avatar

Moderator
Moderator
So someone on Stackoverflow asked this::

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:

Code:
$info = array('Coffee', 'brown', 'caffeine');
The list command will assign each of the array element values to named variables:

Code:
list($drink, $color, $power) = $info;
such that:

Code:
 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.

If I call a function with the field names as parameters, it would need to know where the fields were declared so it can assign the values directly.

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.

and I wanted to share what I came up with in literally ten minutes lol... I wanted to share because after using it and knowing that PHP is far superior to JS that I was amazed it worked correctly! Now there is a sort of strict regulation to the way you can use this.

you can only use this with an Array Array ( array within an array) I haven't added support for simply array nor Object Arrays yet.

So here's the main function

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

Now how do we use it?

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

What this does is gives us freedom of using the variables in a function as dynamic variables later on from an array. It can get more complex but as of now that is it. Really what it is is like a very complex foreach function though we have the freedom to add as many arguments we want in the function for declaring the endless possible "dynamic variables".

Well hopefully someone finds some use for this Very Happy study

View user profile

george

avatar

Admin
Admin
Wow this is amazing @Mr.EasyBB I didn't know you could achieve this with javascript. The logic behind it makes sense and it looks like it would carry out the same functionality. I honestly didn't think it was possible at first.

View user profile http://gareygraphics.com

View previous topic View next topic Back to top  Message [Page 1 of 1]

Share this topic!

URL Direct
BBcode
HTML
JavaScript equivalent of PHPs list function

Permissions in this forum:
You cannot reply to topics in this forum