This question already has answers here:
All possible combinations of a 2d array in Javascript
(2 answers)
Closed 1 year ago.
I want to transform one array of arrays by doing a specific transform, like the following example:
[[1, 2, 3], [4, 5]] => [14, 15, 24, 25, 34, 35];
Note, the first element of the first array is concatenated to each element of the second array to form a new array, and so on with the second element, third... etc
Use array.concat and for loop
let arrays = [
["a1","b1","c1"],
["a2","b2","c2"],
["a1","b1","c1"],
["a1","b1","c1"],
]
let arr = [];
arrays.forEach(array =>{
arr = arr.concat(array)
})
console.log(arr)
Related
This question already has answers here:
Transposing a 2D-array in JavaScript
(25 answers)
Closed 3 months ago.
I have a problem and I am not able to figure this one out.
Lets say we have multiple arrays:
[1,2,3]
[1,2,3]
[11,12,13]
How could I extract all values on each index from multiple arrays and combine them into separate arrays?
Output should be:
[
[1, 1, 11], // all items on index 1
[2, 2, 12], // all items on index 2
[3, 3, 13] // all items on index 3
]
The idea is to get ith elements of all subArrays in every iteration.
for example:
when i = 0, [firstSubArr[0], secondSubArr[0], thirdSubArr[0]] = [1,1,11];
when i = 1, [firstSubArr[1], secondSubArr[1], thirdSubArr[1]] = [2,2,12];
.........
const a = [1,2,3];
const b = [1,2,3];
const c = [11,12,13];
const combined = [a,b,c];
const ans = combined[0].map((_, i) => combined.map(row => row[i])); //Transposing
console.log(ans);
This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 2 years ago.
I have two arrays below,
let array_one = [1, 2, 3, 4, 5]; //first array
let array_two = [5, 6, 7, 8, 9]; //second array
//I need [1, 2, 3, 4, 5, 6, 7, 8, 9]
In array_one and array_two has one common element that is 5 but I need this 5 only one time. I don't need to remove the duplicate elements but need one single element.
Please try this, It should work for you
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
let arr = [...array_one,...array_two];
arr.filter(onlyUnique);
This question already has answers here:
Counting the occurrences / frequency of array elements
(39 answers)
Closed 3 years ago.
Given an array of elements e.g. [9, 9, 9, 2, 5], how can I create a map of element and their frequency in the array [9=>3, 2=>1, 5=>1] using higher order function of array such as map, filter and reduce.
The idea is same as other answer but it doesn't print right map so I am posting correct one.
let arr = [9, 9, 9, 2, 5];
let res = arr.reduce((acc, cur) => (!acc[cur] ? acc[cur] = 1 : acc[cur]++, acc), {})
console.log(res)
This question already has answers here:
sort 2 array with the values of one of them in javascript
(4 answers)
Closed 4 years ago.
Suppose I have two arrays:
arr1 = [64, 23, 35, 11, 55];
arr2 = [34, 10, 54, 12, 4];
If I rearrange (or sort) arr1 then the elements of arr2 should also be rearranged as per the position (or index) of arr1.
For example: if I sort arr1
arr1 = [11, 23, 35, 55, 64];
then the elements in arr2 should be
arr2 : [12, 10, 54, 4, 34 ] (arranged according to index of arr1).
Is it possible? I found that it can be done in same array but I am trying with two different arrays. Thank you for helping.
You could store the original positions before sorting of the first array, then apply that to the second:
// Wrap original positions and values:
const withPos = arr1.map((v, i) => ({v, i}));
// Sort
withPos.sort((a, b) => a.v - b.v);
// Unwrap & apply sort to second array:
arr1 = withPos.map(e => e.v);
arr2 = withPos.map(e => arr2[e.i]);
This question already has answers here:
Summing ; delimited values in Javascript
(1 answer)
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 5 years ago.
I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.
I cannot figure out how to make this work in ES6 javascript.
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
What am I doing wrong? I tried reduce method as well but came up empty