I found compact
function very useful (in php). Here is what it does:
$some_var = 'value';
$ar = compact('some_var');
//now $ar is array('some_var' => 'value')
So it create array from variables which you specify, when key for elements is variable name. Is there any kind of function in javascript ?
compact() function is an inbuilt function in Underscore. js library of JavaScript which is used to return an array after removing all the false values. The false values in JavaScript are NaN, undefined, false, 0, null or an empty string.
The compact() function is an inbuilt function in PHP and it is used to create an array using variables. This function is opposite of extract() function. It creates an associative array whose keys are variable names and their corresponding values are array values. Syntax: array compact("variable 1", "variable 2"...)
The compact() function is used to convert given variable to to array in which the key of the array will be the name of the variable and the value of the array will be the value of the variable.
You can use ES6/ES2015 Object initializer
Example:
let bar = 'bar', foo = 'foo', baz = 'baz'; // declare variables
let obj = {bar, foo, baz}; // use object initializer
console.log(obj);
{bar: 'bar', foo: 'foo', baz: 'baz'} // output
Beware of browsers compatibilities, you always can use Babel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With