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);
Related
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:
Array map function doesn't change elements
(4 answers)
Why does this map function not mutate the values in the original array?
(3 answers)
Closed 2 years ago.
The problem is that I cannot subtract each index of an array from its element(number), using the array.prototype.map() method. I expected the subtraction to be valid, but it is not.
Here is the code:
const whiteSp = [ 5, 11 ];
whiteSp.map(function (ele, i) {
console.log(ele, i) // 5 0, 11 1
console.log(ele - i) // 5, 10
ele = ele - i;
return ele;
});
console.log(whiteSp) // expected [ 5, 10 ], but got [ 5, 11 ]
The second console.log indicates the computation has been made as seen by the value 10, but returns 11 for some reason.
I have also tried 'return ele - i' without its above line, but still does not work.
Ciao, you could try something like this:
let whiteSp = [ 5, 11 ];
whiteSp = whiteSp.map((ele, i) => { return ele - i; });
console.log(whiteSp)
and remember that map function returns a new array so you have to do whiteSp = whiteSp.map....
You need an assignment of the mapped values.
const
whiteSp = [5, 11],
result = whiteSp.map((ele, i) => ele - i);
console.log(result);
This question already has answers here:
Why does a string index in an array not increase the 'length'?
(7 answers)
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 3 years ago.
A strange behavior of array assignment at index: array[<some string>].
Let's say I have a normal array:
const arr = [1, 2, 3]; // undefined
If I type:
arr['.'] = 4; // 4
arr // (3) [1, 2, 3, .: 4]
arr.length // 3
for (a of arr) {
console.log(a)
// 1
// 2
// 3
}
Now I get some strange hybrid between an array and an object.
If I try to spread the array into another variable:
const barr = [...arr] // (3) [1, 2, 3]
Does anyone know what is going on and what rules create this kind of behavior?
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];
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 6 years ago.
There is a javascript array
var arr = [0, 1, 2, 2, 3, 3, 5];
I want to choose elements that repeats twice. In this case its 2 and 3. and i want attach them into a variable.
var a = 2, b = 3;
As far as i know there is no built-in function to do that job. How can i do that. Thanks.
You can use filter to get the values that occur twice.
var arr = [0, 1, 2, 2, 3, 3, 5];
var dups = arr.filter ( (v,i,a) => a.indexOf(v) < i );
console.log(dups);
In comments you stated you would only have doubles, but no values that occur more than twice. Note that the above would return a value more than once, if the latter would be the case.
This returns the values in an array, which is how you should work. To put them in separate values can be done as follows:
var [a, b, ...others] = dups;
...but you would have to know how many variables to reserve for that, and it does not make your further program any easier. JavaScript has many nice functions (methods) for arrays, so you should in fact leave them in an array.
There is no built in function to do that indeed.
You will have to loop thought the array and keeping track of the number of occurrences of the elements, while building a response array.
You could filter a sorted array.
var arr = [0, 1, 2, 2, 3, 3, 5],
repeats = arr.filter(function (a, i, aa) {
return aa[i - 1] === a;
});
console.log(repeats);
Most simple way to do this is the following:
var dups = [];
var arr = [0, 1, 2, 2, 3, 3, 5];
arr.forEach(function (v, i, a){
delete arr[i];
if (arr.indexOf(v) !== -1){
dups.push(v);
}
});
console.log(dups);
It's destructive however.