This question already has answers here:
Finding the sum of subarrays
(6 answers)
Closed 2 years ago.
I have my original array like this
[Array(3), Array(1), Array(3)]
each array has this
[2, 2, 3]
[1]
[2, 2, 2]
I want my final result to be something like this
[7,1,6]
Create a array sum function using Array.reduce(), and then map your array, and apply sum to each sub-array:
const sum = arr => arr.reduce((s, n) => s + n, 0)
const arr = [[2, 2, 3], [1], [2, 2, 2]]
const result = arr.map(sum)
console.log(result)
Related
This question already has answers here:
Sort two arrays the same way
(12 answers)
Closed 18 days ago.
I need to change indexes of elements in arr a due to their indexes in arr b.
const a = [4,3,2,1,5];
const b = [1,2,3,4,5];
console.log(a) [1,2,3,4,5]
If you mean ordering array a according to array b, then you can do like this:
a.forEach((element,i) => {
// first get the index of a[i] from array b
const index = b.indexOf(a[i])
// then swap them
const temp = a[index];
a[index] = a[i];
a[i] = temp;
})
You could sort by using the other array as index. If this daoes not work with real data, please andd a small amount of data to highlight the problem.
const
a = [4, 3, 2, 1, 5],
b = [1, 2, 3, 4, 5];
a.sort((l, r) => b[l - 1] - b[r - 1]);
console.log(...a);
This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed last year.
I have an argument that passes me a 2D array and I want to get the numbers from that array and put them on a normal array.
The argument passed to me = ([1, 3, 2], [5, 2, 1, 4], [2, 1])
What I want to do = [1, 3, 2, 5, 2, 1, 4, 2, 1]
Thanks in advance, I think I'm having a brain fart over here!
You can use the flat method of Array!
const arr = [[1, 2, 3], [4, 5, 6]]
console.log(arr.flat())
// [1, 2, 3, 4, 5, 6]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
Iterate the array and add all data in new array with concat() method.
var arr = [[1, 3, 2], [5, 2, 1, 4], [2, 1]];
var newArr = [];
for(var i = 0; i < arr.length; i++)
{
newArr = newArr.concat(arr[i]);
}
console.log(newArr);
You can use array flat method to convert 2d array to 1d array
const arr = [
[1,2,3],
[4,5,6]
].flat(1); //flat(depth:Number)
console.log(arr);
// [1,2,3,4,5,6]
This question already has answers here:
Sum arrays in array (JavaScript) [duplicate]
(4 answers)
Closed 2 years ago.
I have an array of arrays of integers that looks like this [[5, 3], [2,1], [4, 3]] and the output I am expecting is [8, 3, 7], but I seem to be missing something in my reduce function, as I'm getting an array of n undefined values like [undefined, undefined, undefined] since n=3
How can I get the sum of each array in the arrays and load that into an array?
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const dayArray = [[3,5],[4,6],[8,2]];
const twoWeekArray = dayArray.map((day, index) => {
return day.reduce(reducer);
});
console.log(twoWeekArray);
You forgot to return:
dayArray = [[3,5],[4,6],[8,2]];
twoWeekArray = dayArray.map((day, index) => {
return day.reduce(function (a, b) {
return a + b;
});
});
Here you go...
const subject = [[5, 3], [2,1], [4, 3]]
const result =
subject.map((elem) =>
elem.reduce((acum, current) =>
acum = acum + current,
0
)
)
console.log(result)
This question already has answers here:
Split array into chunks
(73 answers)
Closed 2 years ago.
Suppose that you have an array of doubles in Javascript:
double_arr = [1, 2, 3, 4, 5, 6, 7, 8]
What is the most efficient way to convert it into an array of arrays with 2 doubles like above:
double_arr = [[1,2], [3,4], [5,6], [7,8]]
You can iterate skipping one index in each iteration, like this:
const double_arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = [];
for (let i = 0; i < double_arr.length; i += 2)
result.push([ double_arr[i], double_arr[i+1] ]);
console.log(result);
This question already has answers here:
Split array into chunks
(73 answers)
Closed 3 years ago.
How can I split an array into equal sized arrays:
var a = [1,2,3,4,5,6,7,8];
var b = a.split(2);
// b is equal to [[1,2],[3,4],[5,6],[7,8]];
// one possible way might be something like
[0,1,2,3].map(_ => a.slice(_* 2, _+2));
const chunk = (arr, size) => arr.reduce((carry, _, index, orig) => !(index % size) ? carry.concat([orig.slice(index,index+size)]) : carry, []);
console.log(chunk([1,2,3,4,5,6,7,8], 2))
For the second index for splicing, you need to add one and multiply with the length of the inner arrays.
var a = [1, 2, 3, 4, 5, 6, 7, 8],
b = [0, 1, 2, 3].map(i => a.slice(i * 2, (i + 1) * 2));
console.log(b);