This question already has answers here:
Sum all integers in a multidimensional array javascript
(9 answers)
Closed 5 years ago.
I'm using a nested array like this:
const data = [
[0],
[2],
[[1], 3]
1
]
Is it possible to count all values together. In this example the result should be 7 (0+2+1+3+1).
And is it also possible to count how many arrays are used? This would be 5 arrays
const sumUp = array => array.reduce((sum, el) => sum + (Array.isArray(el) ? sumUp(el) : +el), 0);
This uses a recursive approach with reduce.
Related
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?
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 5 months ago.
I have two array like a = [1,2,3] and b = [2,5] and I want result like result = [5]
How can I do this?
Use Array.prototype.filter
const a = [1,2,3]
const b = [2,5]
const unique = b.filter(e => !a.includes(e))
console.log(unique)
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);
This question already has answers here:
How to create a 2d array of zeroes in javascript?
(5 answers)
Changing one array element affects other elements
(1 answer)
Closed 1 year ago.
I just found something is interesting in Javascript multidimensional array.
let arr = Array(3).fill([]);
arr[0].push('1');
I thought the result should be arr = [['1'], [], []], but got arr = [['1'], ['1'], ['1']].
If update a value as arr[2][0] = '*', expected array to be arr = [['1'], ['1'], ['*']], but got arr = [['*'], ['*'], ['*']].
So why Javascript multidimensional array work like this? How to just update a value in multidimensional array?
From pichard's comment, I got the right result by doing as following:
let arr = Array(3).fill([]).map(obj => { return [] });
arr[0].push('1');
arr[2][0] = '*';
The result is arr = [['1'], [], ['*']]
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);