This question already has an answer here:
Destructure an array parameter [duplicate]
(1 answer)
Closed 2 years ago.
Does any version of Javascript support destructuring of arrays in arrow functions? E.g.
const items = [ [1, 2], [3, 4] ];
const sums = items.map( [a, b] => a + b );
You can, but you have to surround the parameter in parentheses:
const items = [ [1, 2], [3, 4] ];
const sums = items.map(([a, b]) => a + b );
console.log(sums);
Related
This question already has answers here:
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
Closed 1 year ago.
I have the following code, console.log is just to show what happens
function splitToChunks(array, parts) {
let result = new Array(parts).fill([]);
array.map((e,i)=> {console.log("r[",i%parts,"] gets -> ", e )|| result[i%parts].push(e)})
return result;
}
testing with arr = [0...11]
expected result would be:
[
[0, 4, 8],
[1, 5, 9],
[2, 6, 10],
[3, 7, 11],
]
The issue here is that the fill() array method passes by reference, not value, so it's actually the exact same "deep" object being passed to each sub-array, not new ones each time.
A quick workaround for this is to use fill() first and then map() the new array:
function splitToChunks(array, parts) {
let result = Array(parts).fill().map(e => []);
array.map((e,i) => { result[i % parts].push(e) });
return result;
}
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 1 year ago.
This is what I have
function array_diff_very_fast(a, b) {
a = a.filter( el => !b.includes( el ) );
return a;
}
var temp = array_diff_very_fast([1,2,2,2,2,4,5,6], [1,2,6]);
console.log(temp);
and I want it to return [4,5].
I am working in code wars and the function works but it is not efficient enough.
You could take an approach with O(1) for searching the element in a data set, like a Set or an object.
Then omit using a variable just for storing a return value.
function array_diff_very_fast(a, b) {
const bb = new Set(b);
return a.filter(el => !bb.has(el));
}
console.log(array_diff_very_fast([1, 2, 2, 2, 2, 4, 5, 6], [1, 2, 6]));
This question already has answers here:
Create copy of multi-dimensional array, not reference - JavaScript
(4 answers)
Closed 8 months ago.
I want to copy an array of arrays at a different allocation.
I know that I can copy an array in the following way:
a = [[1, 2], [3, 4]]
b = a.slice() // this makes sure that a and b are allocated differently in memory
Now if I change something inside b, then of course,
b[0] = 'abc'
console.log(a, b) // expect a = [[1,2], [3,4]] and b = ['abc', [3,4]]
But when I do the below, a gets changed as well...!
b[0][0] = 'abc'
console.log(a, b) // now it gives a = [['abc', 2], [3, 4]] and b = [['abc', 2], [3, 4]]
Why is this happening, and how can I avoid mutating a?
Thanks so much!
If you know you are only copying 2D arrays you could use a function like the following and avoid using JSON:
function copy2D(array){
result = []
array.forEach((subArray) => {
result.push(subArray.slice())
})
return result
}
One way would be by using map combined with the spread operator. This would be the easiest approach if you can assume that you have a 2D array only
const a = [[1, 2], [3, 4]]
const b= a.map(item => ([...item]))
b[0][0]= "abc"
console.log('a:', a, 'b: ', b)
This question already has answers here:
Limit items in a .map loop
(5 answers)
Closed 3 years ago.
I need to use map starting from the second index. I know how to use loop for with I start from 1 but I want to use map in this case.
Is there a correct way to do this with map?
I'm using JavaScript and ReactJS.
Just slice before you map:
const arr = [1, 2, 3, 4];
const res = arr.slice(1).map(e => e * 2);
console.log(res);
Alternatively, use shift after mapping the full array:
const arr = [1, 2, 3, 4];
const res = arr.slice(1).map(e => e * 2);
res.shift();
console.log(res);
This question already has answers here:
How Es6 rest and spread is working
(1 answer)
Spread Syntax ES6
(8 answers)
Closed 5 years ago.
I have come accross this syntax in a tutorial. Some say its not ES6 syntax. it was in a reduce function.
I need a clear explanation. What is going on in these parentethes ?
{...curr, ...acc}
full code..
const endShape = _(raw)
.filter(({key}) =>!/garbage/.test(key))
.map(({key,value})=>({[key]:value}))
.reduce((acc,curr)=>({...curr, ...acc}));
console.log(endShape);
Spread Operator Shorthand
The spread operator, introduced in ES6, has several use cases that make JavaScript code more efficient and fun to use. It can be used to replace certain array functions. The spread operator is simply a series of three dots.
Longhand
const odd = [1, 3, 5];
const nums = [2 ,4 , 6].concat(odd);
// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = arr.slice();
Shorthand
// joining arrays
const odd = [1, 3, 5 ];
const nums = [2 ,4 , 6, ...odd];
console.log(nums); // [ 2, 4, 6, 1, 3, 5 ]
// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = [...arr];