Passing a specific pre-defined parameter to a function - javascript

Let it be so :
let something = (a , b , c = 0 , d = 0 , e = 0) =>
{console.log(`a is ${a}; b is ${b};c is ${c};d is ${d};e is ${e}`)};`
If I give more then two params to a function, it takes them as respectively.
What if I want to pass the value to "e" variable and skip others, that are predefined?

You can make the params an object.There are two benefits to it, first, you don't have to worry about the order and you won't have to pass a value to c or d.
let something = ({a , b , c = 0 , d = 0 , e = 0}) => {
return `a is ${a}; b is ${b};c is ${c};d is ${d};e is ${e}`;
}
console.log(something({a: 12 , b: 13, e: 51})); //"a is 12; b is 13;c is 0;d is 0;e is 51"

Just pass undefined:
let something = (a, b, c = 0, d = 0, e = 0) => {
console.log(`a is ${a}; b is ${b}; c is ${c}; d is ${d}; e is ${e}`);
};
something(1, 2, undefined, undefined, 3);
You can also wrap the optional parameters in a single argument which will be destructured:
let something = (a, b, {c=0, d=0, e=0} = {}) => {
console.log(`a is ${a}; b is ${b}; c is ${c}; d is ${d}; e is ${e}`);
};
something(1, 2);
something(1, 2, {c: 3});
something(1, 2, {e: 3});

Related

Ignore Calling Argument

Is it possible do not include an argument conditionally when calling a function?
Exemple
Here is a snippet of what behavior I am wanting:
const a=1;
const b="spam";
console.log( a, b!="spam"?b:undefined ); // I would like to ignore b when b=="spam"
// expected output: "1"
// actual output: "1 undefined"
Testings
Here is a snippet representing a logging of every value I have tested so far:
Value
const a="a";
const b=0;
const c=NaN;
const d=null;
const e=undefined;
const f=void 0;
const g="g";
console.log(a,b,c,d,e,f,g);
// expected output: a 0 NaN null g
// actual output: a 0 NaN null undefined undefined g
Filter
const variable = null;
let test = [variable].filter(f => f != null).join();
console.log("start");
console.log(); // no output
console.log(test); // output: "" (type: string)
console.log(typeof test); // output: "string"
console.log(); // no output
console.log("end");
Filter & Apply
const variable=null;
let test = [variable].filter(f => f != variable);
console.log("start");
console.log(test); // output: "(0) []"
console.log.apply(console, test); // no output
console.log.apply(console, null); // no output
console.log("end");
Workaround
That:
const a=1;
const b="spam"
b!="spam"?console.log(a,b):console.log(a);
works, but it is not ignoring an argument at call, it rather test the variable before to then not include it as an argument
Build an array, filter it of unnecessary values, then use the spread syntax ..., like this
const a = "a";
const b = 0;
const c = NaN;
const d = null;
const e = undefined;
const f = void 0;
const g = "g";
console.log(...[a, b, c, d, e, f, g].filter((x) => x !== undefined));
If you want, you can declare as well your console.logDefined function, like this:
console.logDefined = function () {
console.log(...Array.from(arguments).filter((x) => x !== undefined));
};
const [a, b, c, d, e, f, g] = ["a", 0, NaN, null, undefined, void 0, "g"];
console.log( "console.log array >>", a, b, c, d, e, f, g);
console.logDefined("console.logDefined array >>", a, b, c, d, e, f, g);

Deconstruct Object into Array of custom variable names [duplicate]

How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2
You can assign new variable names, like shown in this MDN Example
var o = { p: 42, q: true };
// Assign new variable names
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
So, in your case, the code will be like this
const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2
Online Babel Demo

How to compare 3 value in this way? [duplicate]

This question already has answers here:
Javascript compare 3 values
(7 answers)
Closed 4 years ago.
I need a way to compare 3 values in a short way like this:
'aaa'=='aaa'=='aaa'
false
but as you can see, it doesn't work. Why?
With 2 values it does work obviously:
'aaa'=='aaa'
true
Comparing of first two values evaluates to true and then that true is compared with "aaa" which evaluates to false.
To make it correct you can write:
const a = 'aaa';
const b = 'aaa';
const c = 'aaa';
console.log(a === b && b === c); //true
if you have those strings stored in variables you can do
let a = 'aaa', b = 'aaa', c = 'aaa'
console.log(a === b && b === c) // true
The expression 'aaa'=='aaa'=='aaa' is evaluated as ('aaa'=='aaa')=='aaa'.
The sub-expression in parentheses evaluates to true and it becomes true=='aaa' which is false because when compares two values of different types, JavaScript first converts one of them or both to a common type. It converts the boolean true to the number 1 and the string 'aaa' to the number 0 which are, obviously, not equal.
What you need is
console.log('aaa'=='aaa' && 'aaa'=='aaa')
You can put all values in an Array and then use Array.prototype.every() function to test if all satisfy the condition defined in the callback you pass to it:
let a = 'aaa', b = 'aaa', c = 'aaa'
let arr = [a, b, c]
let arr2 = [1, 2, 1]
console.log(arr.every(i => [arr[0]].includes(i)))
console.log(arr2.every(i => [arr2[0]].includes(i)))
Also, you may get the unique values from a given sequence, and if you get a single element is because all equal:
const same = xs => new Set (xs).size === 1
const x = 'aaa'
const y = 'aaa'
const z = 'aaa'
const areSame = same ([ x, y, z ])
console.log(areSame)
const x_ = 'aaa'
const y_ = 'bbb'
const z_ = 'aaa'
const areSame_ = same ([ x_, y_, z_ ])
console.log (areSame_)
With variadic arguments
const same = (...xs) => new Set (xs).size === 1
const x = 'aaa'
const y = 'aaa'
const z = 'aaa'
const areSame = same (x, y, z)
console.log(areSame)
const x_ = 'aaa'
const y_ = 'bbb'
const z_ = 'aaa'
const areSame_ = same (x_, y_, z_)
console.log (areSame_)

Swapping terms using Assignment de-structuring wont work inside a function

let a = 'alpha', b = 'beta';
[a,b] = [b,a];
This swaps the values of a and b as intended;
but when placed inside a function it doesn't work
let c = 'charlie', d = 'delta';
swapVar = (x,y) => [x,y] = [y,x]
swapVar(c,d);
What am I missing here?
When you do
let a = 'alpha', b = 'beta';
[a,b] = [b,a];
You're swapping the values of a and b.
When you do
let c = 'charlie', d = 'delta';
swapVar = (x,y) => {
// x and y are separate variables scoped within this block
[x,y] = [y,x]
console.log(x,y); // it is swapped alright but isn't reflected on c and d
c = x;
d = y;
// Now the value will have been reflected.
}
swapVar(c,d);
So, within the function the values are swapped but aren't reflected outside. You could modify the program like this:
swapVar = (x,y) => [y,x]
[c, d] = swapVar(c, d); // now you're reflecting the swapped values on the outside
To have the intended effect.
You are doing the swap in a scope where the variables aren't being "exported".
In the first example, you act on the actual variables a and b in the scope they are defined in.
However, in the second example, you are acting on the variables x and y which are the same value as c and d but aren't the actual c and d since they are primitives so the c and d outside the scope of the arrow function is not affected.
{
let a = 'alpha',
b = 'beta';
console.log("Test 1");
console.log(`before a: ${a} b: ${b}`);
[a, b] = [b, a];
console.log(`after a: ${a} b: ${b}`);
}
{
let c = 'charlie',
d = 'delta';
console.log("Test 2");
console.log(`before c: ${c} d: ${d}`);
swapVar = (x, y) => [x, y] = [y, x]
/*
function swapVarExpanded(x, y) {
const tmp = [y, x];
x = tmp[0];
y = tmp[1];
// Doesn't actually matter
// because x and y die at the next closing curly brace due to scope
}
*/
swapVar(c, d);
console.log(`after c: ${c} d: ${d}`);
}

ES6/ES2015 object destructuring and changing target variable

How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2
You can assign new variable names, like shown in this MDN Example
var o = { p: 42, q: true };
// Assign new variable names
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
So, in your case, the code will be like this
const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2
Online Babel Demo

Categories