Is there a function in javascript similar to compact from php?

后端 未结 4 617
醉梦人生
醉梦人生 2021-01-18 15:24

I found compact function very useful (in php). Here is what it does:

$some_var = \'value\';
$ar = compact(\'some_var\');
//now $ar is array(\'so         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-18 15:58

    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

提交回复
热议问题