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);
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:
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))
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));
This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 1 year ago.
so basically i found out that if we create array for example arr1 = [1, 2, 3] and new const { length } = arr1; will going to output 3 but how is this works exactly? is there any more operators instead of { length }? and what are the other use of using const { here some stuff } syntax? i do some research but i didnt find anything that points out this syntax. any assumptions?
Basically this is Object destructuring
Basic example:
const user = {
id: 42,
is_verified: true
};
const {id, is_verified} = user;
console.log(id); // 42
console.log(is_verified); // true
In your case:
const arr1 = [1, 2, 3]
arr1.length = 3
// Or,
const { length } = arr1
To know more: Object destructuring
Let examine each line of code :)
const arr1 = [1,2,3];
Here, you define an array "arr1", which has 3 elements, so arr1's length is 3.
In javascript, arr1 is an object, and 1 of its properties is "length", which reflects the length of the array. So, arr1.length = 3
Now the second line :
const {length} = arr1
Here, we use destructuring assignment to get the property "length" from object arr1. This line is equal to :
const length = arr1.length;
which give you length = 3
Make sense?
You can read more about the destructuring assignment here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 6 years ago.
I have an array of objects:
var arr = [
{number: "AL-32021611", b: "7500"},
{number: "AL-32021612", b: "Continental"},
{number: "AL-32021612", b: "R3"},
{number: "AL-32021612", b: "7500"}
];
Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?
for example
//loop
result = ["Continental", "R3", "7500"]
what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences
Using ES6 features:
let result = Array.from(new Set(arr.map(el => el.b)));
or
let result = [...new Set(arr.map(el => el.b))];
Array.from()
Set
Array.prototype.map()
Arrow Functions
Spread Operator ...
Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.
As you have it,
result = {"Continental", "R3" , "7500"};
is not a valid object. You could use a for loop and push the b values into a new array that would look like:
result = ["Continental", "R3" , "7500"];
Your for loop would look like:
var result = [];
for(var n of arr) {
result.push(arr[n].b);
}
return result;