How to combine two array by underscorejs [duplicate] - javascript

This question already has answers here:
Cartesian product of multiple arrays in JavaScript
(35 answers)
Closed 1 year ago.
I am new for underscorejs,
First I have two arrays =>
var array1 = [1,2];
var array2 = [1,2];
How can I get like =>
[[1,1],[1,2],[2,1],[2,2]]
by using underscorejs?

What you are trying to achieve is called Cartesian Product. Underscore is not required, nor it has such util function.
a.map(_a => b.map(_b => [_a, _b])).flat()
Or
a.flatMap(_a => b.map(_b => [_a, _b]));
const a = [1, 2], b = [1, 2];
const out = a.map(_a => b.map(_b => [_a, _b])).flat();
console.log(JSON.stringify(out));

Related

Extract all values on each index from multiple arrays and combine them into separate arrays [duplicate]

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);

How to get from two arrays and return unique values from last array javascript [duplicate]

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)

return reordered version of array b based on reordered version of a [duplicate]

This question already has answers here:
Javascript - sort array based on another array
(26 answers)
Closed last year.
I have two arrays a and b like this:
const a = [1,2,3,4]
const b = ['1','2','3','4'] // could be 'a','b','c','d'
const reordered_a = [4,1,3,2] // based on this reordering
function reorder_b() {
// should return ['4','1','3','2']
}
How can I return reordered version of b based on reordered positions in reordered_a?
You could take an object with the indices for the values and map the pattern with the values of the indices.
const a = [1, 2, 3, 4]
const b = ['1', '2', '3', '4'] // could be 'a','b','c','d'
const reordered_a = [4, 1, 3, 2] // based on this reordering
function reorder_b() {
const references = Object.fromEntries(Object.entries(a).map(a => a.reverse()));
return reordered_a.map(k => b[references[k]]);
}
console.log(reorder_b()); // 4 1 3 2
What you got to do is first use every pair of elements el1 and el2 and convert them to numbers. Then once they're converted, make sure that the latter is bigger than the latter by checking their difference :
return b.sort((el1,el2)=>Number(el2)-Number(el1))

Is there any way to plus multi array number in javascript? [duplicate]

This question already has answers here:
How to calculate the sum of multiple arrays?
(6 answers)
Closed 1 year ago.
I have an array. With each item in array is an array number. And the length of each array is the same. For example:
var data = [[1,2,4,1], [2,2,1,3], [1,1,2,2], ...]
And the result I want to have:
=> res = [4, 5, 7, 6]
res is the result of adding arrays according to the corresponding index. And of course my data may also contain lots of items.
I have referenced through the lodash.unzipWith. But it doesn't seem viable. With any advice. please let me know. Sorry for my weak English
You can use reduce and write something like this, without lodash or anything
const data = [[1,2,4,1], [2,2,1,3], [1,1,2,2]]
const sumArrs = (arrs) => {
return arrs.reduce((prev, curr) => {
return curr.map((num, i) => num + (prev[i] || 0))
}, [])
}
console.log(sumArrs(data))

Merge 2 arrays in change [duplicate]

This question already has answers here:
Zip arrays in JavaScript?
(5 answers)
Closed 3 years ago.
I wanted to merge two arrays like you can fold 2 packs of cards - in ugly code with for loop and assuming same length so no if's for safety it would look like this:
const arr = [1,2,3];
const rra = [4,5,6];
const result = [];
for(let i=0; i<arr.length; i++){
result.push(arr[i],rra[i]);
}
console.log(result); // Array(6) [ 1, 4, 2, 5, 3, 6 ]
I know there is something similar in String.raw() but it cuts off last element and returns a string, is there equivalent in array ?
String.raw({raw: [1,2,3]}, ...[4,5,6]); //"14253"
You can use .flatMap() for this - This is the same as .map(), followed by .flat()
const arr = [1,2,3];
const rra = [4,5,6];
let newArray = arr.flatMap((ele, i) => [ele, rra[i]]);
console.log(newArray);

Categories