This question already has answers here:
Split array into chunks
(73 answers)
How to put each element of array into table row - table data?
(1 answer)
Closed 3 years ago.
sorry I don't speak English very well.
I have a table with several elements, I want to return all the X elements a new table inside a table.
For example:
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let newArr = [
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
];
This is the way I am currently doing this :
const size = shuffleImages.length / 4
while (shuffleImages.length > 0) {
columns.push(shuffleImages.splice(0, size))
}
I would like to understand how to do this without using while and instead use for example .map or is there just an easier way to do this?
You need to split the array into small array using map and than slice it :
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var chunkSize = 3;
const groups = arr.map( function(e,i){
return i%chunkSize===0 ? arr.slice(i,i+chunkSize) : null;
}).filter((e) =>e);
console.log(groups);
Related
This question already has answers here:
Rotate the elements in an array in JavaScript
(42 answers)
Closed 1 year ago.
let's say I have this array of number in reverse order:
var days = [6, 5, 4, 3, 2, 1, 0];
and I have this variable:
var startingDay = 4;
How can I sort the array so that the array is like this
console.log(days == [4, 3, 2, 1, 0, 6, 5]);
>> true
??
Thanks!
You can rotate the array by slicing and concatenating the two parts.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
var idx = days.indexOf(day);
var res = days.slice(idx).concat(days.slice(0, idx));
console.log(res);
You can also use unshift and splice to modify the array in-place.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
days.unshift(...days.splice(days.indexOf(day), days.length));
console.log(days);
This question already has answers here:
Preserve Nested Array Structure When Converting to String, JavaScript
(2 answers)
Closed 2 years ago.
my question is, if I have an array in javascript, let's say :
let x = [1, 2, 3, [4, 5, 6]]
And I want to transform it into a string that would look like this : y = "[1, 2, 3, [4, 5, 6]]", how should I do it?
I tried this things:
let a = x.toString();
let b = y.toLocaleString();
let c = new String(x);
But the problem with it is that, all of them look like this:
"1, 2, 3, 4, 5, 6"
So, it completely removes the '[]'.
How can I keep the array inside the string like this :
"[1, 2, 3, [4, 5, 6]]"
with the []
Does this helps?
let y = [1, 2, 3, [4, 5, 6]];
var x = JSON.stringify(y);
console.log(typeof(x));
console.log(JSON.stringify(y));
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:
Group an array to form three arrays based on the index
(3 answers)
Closed 3 years ago.
Trying to do a method to re-arrange an array to print in columns:
I have:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I need to have:
b = [1, 4, 7];
c = [2, 5, 8];
d = [3, 6, 9];
...
where numbers of others colums(b, c, d) could be variable.
Use an Array#reduce call that returns an array of arrays, and determine which array each item belongs to, using the remainder of the index.
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var listCount = 3;
var [b, c, d] = a.reduce(function(lists, item, index) {
lists[index % listCount] = lists[index % listCount] || [];
lists[index % listCount].push(item);
return lists;
}, []);
console.log(b);
console.log(c);
console.log(d);
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Compute intersection of two arrays in JavaScript [duplicate]
(4 answers)
Closed 5 years ago.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1.)If elements are equals,show that common elemenst in output
2.)The output(common elements) should be array form
Use Array#filter method and inside filter function use Array#indexOf or Array#includes methods to check second array includes the element.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = array1.filter(function(v) { // iterate over the array
// check element present in the second array
return array2.indexOf(v) > -1;
// or array2.includes(v)
})
console.log(res);