Why is this map empty after array.reduce [duplicate] - javascript

This question already has answers here:
How do you JSON.stringify an ES6 Map?
(16 answers)
How to serialize a Map in javascript?
(3 answers)
Closed last year.
Why does this map print empty? I'd expect it to be populated from the array? I'm probably missing something small.
let numbers = [1, 2, 3];
let numberMap = numbers.reduce((map, obj) => {
return map.set(obj, obj + 1);
}, new Map());
console.log(JSON.stringify(numberMap));

Related

Creating a copy of an array with specific index removed [duplicate]

This question already has answers here:
Why doesn't splicing an object from an array in Javascript return the array?
(4 answers)
How to Clone an Array With a Removed Element? [duplicate]
(2 answers)
Closed 21 days ago.
Why does the new array only contain the value '394', instead of all the other ones?
const values = [5, 11, 394, 2, 576];
function pureSplice(givenArray: number[]){
const newArray: number[] = givenArray.splice(2, 1).map(x => x);
return newArray;
}
pureSplice(values);
The 'newArray' only contains the value '394', why does it do that and is there a way to get the array to have all values other than 394' with .splice and .map?

Array is not changed after filter on javascript [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Filter Array modifying the original array itself [duplicate]
(3 answers)
Closed 3 months ago.
I'm trying yo filter the numeric values of an array with this code:
function getNumerics(toFilter) {
toFilter = toFilter.filter( element => !isNaN(element));
console.log(toFilter);
}
var toFilter = [1, 'z', '4', 2, 6];
getNumerics(toFilter);
console.log(toFilter);
The console.log inside the function shows a correct result but but the last console.log show the array with all the values but if I pass the array to the function why is not change? in javascript all parameters are passed are reference , aren't it?
Your function should return the filtered Array:
function getNumerics(toFilter) {
return toFilter.filter( element => !isNaN(element));
}
var toFilter = [1, 'z', '4', 2, 6];
toFilter = getNumerics(toFilter);
console.log(toFilter);

traversing a multiarray with Javascript [duplicate]

This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Zip arrays in JavaScript?
(5 answers)
Transposing a 2D-array in JavaScript
(25 answers)
Closed 1 year ago.
Hi guys I have this array :
[[2,3,0],[0,4,5]]
and i want traverse this array like:
[[2,0],
[3,4],
[0,5]]
Any recommendations please? I am working with javascript
You can easily achieve this result using the Array.prototype.map
const arr = [
[2, 3, 0],
[0, 4, 5],
];
const result = arr[0].map((val, i) => {
return Array(arr.length)
.fill("")
.map((_, index) => arr[index][i]);
});
console.log(result);

How to convert array to array object,like['in','gs'] to [{"state":"in"},{"state":"gs"}] in javascript? [duplicate]

This question already has answers here:
JS : Convert Array of Strings to Array of Objects
(1 answer)
Javascript string array to object [duplicate]
(4 answers)
Closed 1 year ago.
How to convert array to array object,like
['in','gs'] to [{"state":"in"},{"state":"gs"}]
map over it and return a new array
let arr = ['in','gs']
let newArr = arr.map(item => ({state: item}));
console.log(newArr);

ReactJs Combine Key and Value [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
merge two arrays (keys and values) into an object [duplicate]
(3 answers)
Closed 2 years ago.
Platform: ReactJS
I am trying to combine the two arrays as a date:value object. Any thoughts?
a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
b = [ 1, 2, 3, 4 ]
I am looking for the following result:
["1/1/2020":1, "1/2/2020":2, "1/3/2020":3, "1/4/2020":4]
Thank you,
You can try this approach.
const a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
const b = [ 1, 2, 3, 4 ]
const c = {};
a.forEach((v, i) => {
c[v] = b[i]
});
console.log(c);

Categories