Why spread operator turn my array into numbers - javascript

i have
a = [[1,2,3],[4,5,6]]
How come when i write
console.log(...a.shift())
it gives me 1 2 3
but not 1,2,3 nor [1, 2, 3]
can anyone explain me the mechanism behind this?

a.shift() returns the first element of the array, which is [1, 2, 3]. So your code is equivalent to:
console.log(...[1, 2, 3])
The spread syntax causes each element of the array to become a separate argument, so this is equivalent to
console.log(1, 2, 3)
which prints each number separately on the console.
To get [1, 2, 3] you shouldn't use ..., just write
console.log(a.shift())
To get 1,2,3 use
console.log(a.shift().join(','))

console.log(...a.shift())
is run in given order:
a.shift() returns [1, 2, 3] => console.log(...[1, 2, 3])
...[1, 2, 3] is evaluated into 1 2 3 and passed to console.log as 3 different arguments => console.log(1, 2, 3)
Which has out of 1 2 3

Related

Sorting array with sort function [duplicate]

This question already has answers here:
How does Javascript's sort() work?
(8 answers)
How does sort function work in JavaScript, along with compare function
(7 answers)
How does JavaScript's sort (compareFunction) work? [duplicate]
(3 answers)
Closed 1 year ago.
I got question today, that I couldn't answer. I would appreciate if you could just explain it to me. Why my array doesn't output [1, 2, 3, 4, 5] but only [2, 3, 4, 5]?
This is the code:
let numbers = [1, 2, 3, 4, 5]
let order = numbers.sort((a, b) => {
console.log(a);
});
But when I use this code, it works perfectly fine
let numbers = [1, 2, 3, 4, 5]
let order = numbers.sort((a, b) => {
return a;
})
console.log(order);
It doesn't output [2, 3, 4, 5] it outputs 2, 3, 4 and 5 each in a separate step of the iteration. If you also log order you'll see that the array still contains [1, 2, 3, 4, 5]. The comparator function receives two elements of the array that it compares to each other. That way it knows in which order those elements should be. If you only log a and not b, you'll wont see all the information that is passed to the comparator. If you log both a and b, you'll see that also 1 is passed, to parameter b.
let numbers = [1, 2, 3, 4, 5]
let order = numbers.sort((a, b) => {
console.log('Comparing', a, b);
});
console.log('Final result', order);
Keep in mind that the comparator function should return either a positive number when a is greater than b, a negative number when a is less than b and 0 when both elements are equal to each other. The above comparator function doesn't return any value at all, so it might not do what you expect it to do.
(Also your snippet that "works perfectly fine" doesn't follow that contract. It returns a positive number for each element, so it is basically saying that every element is greater than every other element.)
The sort() method sorts the elements of an array in place and returns the sorted array.
[1, 2, 3, 4, 5].sort((a, b) => {
console.log({a, b}); // a is secondItem, b is firstItem of an array
return -1; // return -ve value for DESC and truthy value for ASC order
});
Output will be
{a: 2, b: 1}
{a: 3, b: 2}
{a: 4, b: 3}
{a: 5, b: 4}
sort output: [5, 4, 3, 2, 1]

Calling Array.prototype.map with Math.max returns array of NaN [duplicate]

This question already has answers here:
Why does parseInt yield NaN with Array#map?
(8 answers)
Closed 4 years ago.
Why does [1, 2, 3].map((x) => Math.max(x)) return [1, 2, 3]
yet [1, 2, 3].map(Math.max) returns [NaN, NaN, NaN] ?
I would have thought since [1, 2, 3].map(Number) returns [1, 2, 3], the same would true for using Math.max in the same way.
Especially since Math.max(3) == Number(3).
It's because map invokes the callback with three arguments:
The current array element.
The index of the current element.
The array map was called on.
So [1, 2, 3].map(Math.max) is not equivalent to [1, 2, 3].map((x) => Math.max(x)), but to [1, 2, 3].map((x, i, arr) => Math.max(x, i, arr)).
And Math.max(1, 0, [1, 2, 3]) is NaN because the [1, 2, 3] array becomes "1,2,3" when stringified, which becomes NaN when converted to a number.
You can use spread operator:Math.max(...[1,2,3])

Array.From() different results

if you run this javascript on this page:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
you get the following result:
console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]
> Array [2, 4, 6]
if you run the same code in the chrome console on this page(https://portal.fellowshipone.com/) i get this result:
console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]
VM2786:1 (3) [1, 2, 3]
Why does this occur?
The page you are referring to is using Array.from from the Prototype library. Try to enter Array.from to the console and you will see that this function is declared in an external library which overrides native implementation.

Difference between curry and curryRight in Lodash

What is the difference between curry and curryRight in Lodash?
Is just the application order of the provided arguments switched from f(a,b,c) which applies to f(a) -> f(b) -> f(c) to f(a,b,c) which then applies to f(c) -> f(b) -> f(a)?
I already looked into the Lodash documentation but that didn‘t helped me.
From the documentation:
var abc = function(a, b, c) {
return [a, b, c];
};
var curried = _.curryRight(abc);
curried(3)(2)(1);
// => [1, 2, 3]
curried(2, 3)(1);
// => [1, 2, 3]
curried(1, 2, 3);
// => [1, 2, 3]
The first example is simple. The order of the arguments is reversed (in comparison to _.curry).
The second and third one can perhaps be confusing.
In the third example we see that the order of the arguments is NOT reversed. That is because only the currying is applied in reverse. In other words the parentheses are applied in the reverse order, but what is inside of the parentheses sustains the original order.
Compare this to the result of _.curry(_.flip(f)):
var abc = function(a, b, c) {
  return [a, b, c];
};
var curried = _.curry(_.flip(abc), 3);
 
curried(3)(2)(1);
// => [1, 2, 3]
curried(3, 2)(1);
// => [1, 2, 3]
curried(3, 2, 1);
// => [1, 2, 3]
As you can see the result is different. Now the order of the arguments is reversed totally in all the examples.
Btw notice that for some reason I needed to specify the arity to 3 in _.curry(_.flip(abc), 3);. I don't know why but it causes an exception without that.

underscore.js - Is there a function that produces an array thats the difference of two arrays?

Looking for a function in underscore.js that will take 2 arrays and return a new array of unique values? Something like _without
_.without([0, 1, 3, 9], [1, 3]);
I would like => [0,9] returned
It appears _without's 2nd arg is a list of values, not an array. Anyone out there know
if underscore has the specific function I'm looking for? Or can I take an exisitng array and covert it to values the function expects.
Thanks,
~ck in San Diego
The _.difference function should give you what you're looking for:
_.difference([0, 1, 3, 9], [1, 3]); // => [0, 9]
_.without.apply(_, [arr1].concat(arr2))
[[0, 1, 3, 9]].concat([1, 3]) is [[0, 1, 3, 9], 1, 3];
_.without.apply(_, [[0, 1, 3, 9], 1, 3]) is _.without([0, 1, 3, 9], 1, 3)
You've got a perfectly good _.without method. So just convert an array into a list of values you can pass into a function. This is the purpose of Function.prototype.apply
var result = _.reject([0, 1, 3, 9], function(num) {
return _.include([1, 3], num);
});
Lo-Dash is extended Underscore and here is what you need: http://lodash.com/docs#xor
_.xor
Creates an array that is the symmetric difference of the provided arrays. See http://en.wikipedia.org/wiki/Symmetric_difference.
_.xor([1, 2, 3], [5, 2, 1, 4]);
// → [3, 5, 4]

Categories

Resources