Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP equivalent of list and explode in javascript [duplicate]

Possible Duplicate:
Javascript equivalent of PHP’s list()

I want an equivalent of this line but in JavaScript :

<?php list($Id, $Iduser, $Idmany) = explode("," , $IdALL); ?>

Where :

$IdALL = '1, 2, 3';

Is there any way to do that with JavaScript and that could be supported on all modern browsers ?

like image 295
Sami El Hilali Avatar asked Dec 01 '12 11:12

Sami El Hilali


2 Answers

var values = "1, 2, 3".split(", ");
var keys = ["Id", "Iduser", "Idmany"];
var final = {};

for(var i = 0; i < values.length; i++){
    final[keys[i]] = values[i];
}

Of course, at the expense of readability you could do all this in the for statement:

for(var i = 0, values = "1, 2, 3".split(", "), keys = ["Id", "Iduser", "Idmany"], final = {}; i < values.length; i++){
    final[keys[i]] = values[i];
}

The variables can then be accessed as follows:

alert(final.Id);
alert(final.Iduser);
alert(final.Idmany);
like image 75
Asad Saeeduddin Avatar answered Oct 12 '22 22:10

Asad Saeeduddin


Please see the site: http://phpjs.org/functions/

Here you want all the functions available in php to js.

like image 39
Chirag Patel Avatar answered Oct 13 '22 00:10

Chirag Patel