Destructuring in Node.Js

Destructuring in Node.JS

Update for node v6 and newer: Node v6 supports destructuring assignment without anything special needed:

var [a, b] = [1, 2];

For older versions of node: You can get the list of supported harmony features by typing:

node --v8-options | grep harmony

node 5.x will give you:

--es_staging (enable all completed harmony features)
--harmony (enable all completed harmony features)
--harmony_shipping (enable all shipped harmony fetaures)
--harmony_modules (enable "harmony modules" (in progress))
--harmony_regexps (enable "harmony regular expression extensions" (in progress))
--harmony_proxies (enable "harmony proxies" (in progress))
--harmony_sloppy_function (enable "harmony sloppy function block scoping" (in progress))
--harmony_sloppy_let (enable "harmony let in sloppy mode" (in progress))
--harmony_unicode_regexps (enable "harmony unicode regexps" (in progress))
--harmony_reflect (enable "harmony Reflect API" (in progress))
--harmony_destructuring (enable "harmony destructuring" (in progress))
--harmony_default_parameters (enable "harmony default parameters" (in progress))
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
--harmony_atomics (enable "harmony atomics" (in progress))
--harmony_simd (enable "harmony simd" (in progress))
--harmony_array_includes (enable "harmony Array.prototype.includes")
--harmony_tostring (enable "harmony toString")
--harmony_concat_spreadable (enable "harmony isConcatSpreadable")
--harmony_rest_parameters (enable "harmony rest parameters")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_arrow_functions (enable "harmony arrow functions")
--harmony_new_target (enable "harmony new.target")
--harmony_object_observe (enable "harmony Object.observe")
--harmony_spreadcalls (enable "harmony spread-calls")
--harmony_spread_arrays (enable "harmony spread in array literals")
--harmony_object (enable "harmony Object methods")

The flag you need, --harmony_destructuring, was added in Node 4.1. Currently, you'll need to pass the --harmony_destructuring flag to enable the feature:

$ node --harmony_destructuring
> var {foo} = {foo: 'bar'};
undefined
> foo
'bar'

Why does destructuring on boolean is working in javascript?

What you're using here isn't destructuring, it's object property spread. It requires that the expression to the right-hand side (RHS) of the ... evaluates to either an object or something that can be converted to one (with exceptions for null and undefined). It then takes the own-enumerable properties from the object and copies them to the object being created. In the case of your example, two possibilities can occur for the below code:

...(condition && toto)
  1. condition is true, and so the above expression evaluates to the object toto. It doesn't evaluate to a boolean, but rather the last truthy value, which in your case is the object toto. The object property spread syntax then takes the own-enumerable properties from the object and adds them to the object being created.

  2. condition is false, and so the above expression evaluates to the value false. This means that JavaScript treats the above code as ...false. As false is a primitive and not an object, JavaScript tries to convert it to one by wrapping it in a boolean object:

    ...new Boolean(false)

    now that the RHS of the ... has been converted to an object, JavaScript then takes the own-enumerable properties of the boolean object and adds them to the object being created. As the boolean doesn't have any own properties which are enumerable, nothing is added to the created object.

Why object destructuring in javascript will affect scope?

It's not exactly right

function log() {
console.log('na aahh')
}
(function() {
var o = {
add: function(a , b) {
return a + b;
},
log: function () {
console.log('hi, i am locally scoped');
}
}
var { log } = o;
o.log();
log();
})();

Object destructuring with default parameters in Node.js 8.9.4

You have to use assignment wihout declaration

let x;
({k1: x = null } = {k1: "Hello"});

or just:

let { k1: x = null } = { k1: "Hello" };

The round braces ( ... ) around the assignment statement is required
syntax when using object literal destructuring assignment without a
declaration.

{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b}
on the left-hand side is considered a block and not an object literal.

However, ({a, b} = {a: 1, b: 2}) is valid, as is var {a, b} = {a: 1,
b: 2}

NOTE: Your ( ... ) expression needs to be preceded by a semicolon or
it may be used to execute a function on the previous line.

nodejs destructuring variables returning undefined

Here:

class PubSub {
constructor({ blockchain, transactionPool, wallet }) {

and here:

const testPubSub = new PubSub()

So, for this specific constructor call, the destructuring in the PubSub constructor amounts to

const { blockchain, transactionPool, wallet } = undefined;

And you can't do that, neither with undefined nor with null. You need to either, call new PubSub(/*...*/) with correct parameters, or maybe use default parameter values.

Destructuring Assignment to Pass an object as function's parameter

Of course the function doesn't mention stats explicitly - the function cannot know the name of a variable that might be passed to it

const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};

const half = ({ max, min }) => (max + min) / 2.0;

console.log(half(stats));

JS array of objects destructuring?

Try something like below using map, Object destructuring and spread operator

const data = [{
"id": 101,
"title": {
"rendered": "CTC20180018"
},
"acf": {
"fielda": "valuea",
"fieldb": "valueb",
"fieldc": "valuec"
}
},
{
"id": 102,
"title": {
"rendered": "D2021063365"
},
"acf": {
"fielda": "valuea",
"fieldb": "valueb",
"fieldc": "valuec"
}
}
]

const result = data.map(({
id,
title,
acf
}) => ({
id: id,
title: title.rendered,
...acf
}));

console.log(result);


Related Topics



Leave a reply



Submit