Javascript equivalent of PHP's list()
There is, in 'newer' versions of Javascript: Destructuring assignment - Javascript 1.7. It's probably only supported in Mozilla-based browsers, and maybe in Rhino.
var a = 1;
var b = 3;
[a, b] = [b, a];
EDIT: actually it wouldn't surprise me if the V8 Javascript library (and thus Chrome) supports this. But don't count on it either
Now supported in all modern browsers(except IE, of course).
Does JavaScript have a language construct similar to the php list command?
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
JavaScript construct equivalent to PHP's list()?
People seem to hate the with() construct in javascript, but anyway...
function f(){return {a:1, b:2};}
with(f()) {
alert(a);//1
}
// or
function combine(propertyNames, values) {
var o = {};
for (var i=0; i<propertyNames.length; i++) {
o[propertyNames[i]] = values[i];
}
return o;
}
with (combine(['a', 'b'], [1, 2])) {
alert(b);//2
}
PHP equivalent of list and explode in javascript
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);
JavaScript equivalent of PHP $array[$i][]
JavaScript doesn't magically create an array if there is none. You have to create it yourself. So it would be something like
$([1, 2, 3, 4, 4, 4, 6, 6, 2, 3]).each(function(key, value) {
if (levels[value] == null) {
levels[value] = [];
}
levels[value].push(value);
});
Learn more about arrays.
JavaScript equivalent of PHP's in_array()
No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples.
jQuery's implementation of it is as simple as you might expect:
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
If you are dealing with a sane amount of array elements the above will do the trick nicely.
EDIT: Whoops. I didn't even notice you wanted to see if an array was inside another. According to the PHP documentation this is the expected behavior of PHP's in_array
:
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
if (in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (in_array('o', $a)) {
echo "'o' was found\n";
}
// Output:
// 'ph' was found
// 'o' was found
The code posted by Chris and Alex does not follow this behavior. Alex's is the official version of Prototype's indexOf, and Chris's is more like PHP's array_intersect
. This does what you want:
function arrayCompare(a1, a2) {
if (a1.length != a2.length) return false;
var length = a2.length;
for (var i = 0; i < length; i++) {
if (a1[i] !== a2[i]) return false;
}
return true;
}
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(typeof haystack[i] == 'object') {
if(arrayCompare(haystack[i], needle)) return true;
} else {
if(haystack[i] == needle) return true;
}
}
return false;
}
And this my test of the above on it:
var a = [['p','h'],['p','r'],'o'];
if(inArray(['p','h'], a)) {
alert('ph was found');
}
if(inArray(['f','i'], a)) {
alert('fi was found');
}
if(inArray('o', a)) {
alert('o was found');
}
// Results:
// alerts 'ph' was found
// alerts 'o' was found
Note that I intentionally did not extend the Array prototype as it is generally a bad idea to do so.
What is PHP's equivalent of JavaScript's array.every()?
Use a for loop with an early return.
PHP does not have a native function that performs the same function as Javascript's array#every.
JavaScript equivalent of php DOMDocument Object
Use DOMParser()
Your ported code, which is very much the same as your PHP:
let parser = new DOMParser()
let doc = parser.parseFromString(`<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ul>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>`, "text/html")
let lis = doc.getElementsByTagName('li')
let items = []
for (let i = 0; i < lis.length; i++) items.push(lis[i].textContent)
console.log(items)
Related Topics
Pkill -F Doesn't Work for Process Killing
Two Simultaneous Ajax Requests Won't Run in Parallel
PHP Include Error Not Findind the Path
Laravel Eloquent Sum of Relation's Column
How to Get the Client's Ip Address in a PHP Webservice
How to Convert JSON to Xml in PHP
Why Are "Echo" Short Tags Permanently Enabled as of PHP 5.4
Automatic Image Format Detection in PHP
Check Whether a Request Is Get or Post
How to Enable Memcache in Wamp
Execute Commands on Remote MAChine via PHP
Mysql_Fetch_Array() Expects Parameter 1 to Be Resource Problem
Difference Between Array_Map, Array_Walk and Array_Filter
Best Way to Determine If a Url Is an Image in PHP
Laravel: Connect to Databases Dynamically
How to Logout from a Session in PHP