How can I populate array of array [duplicate] - javascript

This question already has answers here:
Partitioning in JavaScript [duplicate]
(7 answers)
Split array into chunks
(73 answers)
Closed 9 years ago.
I want to convert an array to an array of arrays.
For example if my array is:
a = [1,2,3,4,5,6]
I want:
b = [[1,2],[3,4],[5,6]]
How can I do this?

function Create2DArray(rows) {
var arr = [];
for (var i=0;i<rows;i++) {
arr[i] = [];
}
return arr;
}

Related

Array to multidimensional array [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 1 year ago.
I am trying to convert a simple array to a multidimensional array.
The Array I currently have
Array = (1, 2, 3, 4);
I would like to convert this Array to be like this
Array = ([1, 2], [3, 4]);
Any help is welcome, thanks already.
This worked for me:
function TwoDimensional(arr, size)
{
var res = [];
for(var i=0;i < arr.length;i = i+size)
res.push(arr.slice(i,i+size));
console.log(res);
}
Pretty easy when it works! Thanks

How to find two arrays differences and form differences in new array in JavaScript? [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
How do I get differences between 2 arrays and form differences in new array?
arr1 = [1,2,3,4,5];
arr2 = [1,2,3,4];
newArr = [5];
Answer must be the same when arr1 and arr2 switch places.
We can use .includes()/ .indexOf() to resolve this q.
function diffArr(arr1, arr2) {
return arr1
.concat(arr2) //Join 2 arr into 1 new array
.filter(item => !arr1.includes(item) || !arr2.includes(item));
} //compare and remove arr1 & 2 with new array

How to check value in the array is present in the array variable or not? [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
JavaScript is in array [duplicate]
(16 answers)
Closed 3 years ago.
I wanted to achieve a value in the array is present in the array variable or not.
var a = [1, 2, 3];
if (a === 1) {
alert("green");
}
So my goal is to check in the variable a holds the value 1 or not.
Use includes:
let a = [1, 2, 3]
if (a.includes(1))
console.log('exist');
else
console.log('not exist');
You need to loop through array members and check if any of the members has that value. Here's an example:
var a = [1,2,3];
for(let i = 0; i < a.length; i++){
if(a[i] == 1){
alert("green");
}
}

optimal solution to filter elements from array [duplicate]

This question already has answers here:
Remove all elements contained in another array [duplicate]
(17 answers)
Closed 5 years ago.
I have an array of animals arr = ['cat','dog','elephant','lion','tiger','mouse']
I want to write a function remove(['dog','lion']) which can remove the elements from arr, and returns a new array, what is the best and optimal solution?
example:
arr = ['cat','dog','elephant','lion','tiger','mouse']
remove(['cat', 'lion'])
arr should get changed to
arr = ['dog','elephant','tiger','mouse']
Note: No Mutations
you can just use filter()
var arr = ['cat','dog','elephant','lion','tiger','mouse'];
var newArr = arr.filter(x => !['cat', 'lion'].includes(x))
console.log(newArr);

remove duplicates from two arrays javascript [duplicate]

This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 8 years ago.
I have two arrays in javascript ,And I wand to remove duplicates from them
var array1 = [1,2,3,4,5,6,7,8,9];
var array2 = [3,5,7,11,17,19];
I want the output to be [1,2,3,4,5,6,7,8,9,11,17,19]
var array1 = [1,2,3,4,5,6,7,8,9], array2 = [3,5,7,11,17,19];
var output = array1.concat(array2);
var output = output.filter(function (item, pos) {return output.indexOf(item) == pos});
DEMO

Categories

Resources