Array to multidimensional array [duplicate] - javascript

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

Related

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

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

How can I compare two JavaScript arrays in order to determine if they have identical values? [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 8 years ago.
I don't want to compare the two arrays as a whole, I specifically want to check if the 2nd array has any values that match a value in the first one. I then want to return the value that matches in both.
If I'm not mistaken, comparing two arrays as a whole would be done like this:
array1 = [1,2,3];
array2 = [1,3,4];
console.log(JSON.encode(array1)==JSON.encode(array2));
So in this case, I would want to check if array2 has any matching values to array one, not if they both are equivalent. Any help is appreciated!
var array1 = [1, 2, 3],
array2 = [1, 3, 4];
var AnyItemsOfArray1InArray2 = array1.some(function(item) {
return array2.indexOf(item) > -1;
});
console.log(AnyItemsOfArray1InArray2);
var ItemsOfArray1InArray2 = array1.filter(function(item) {
return array2.indexOf(item) > -1;
});
console.log(ItemsOfArray1InArray2);

How can I populate array of array [duplicate]

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

JS or jQuery - Remove element from an array [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove item from array by value | JavaScript
Is there a way, in JS or with help of jQuery, to remove element from an array with knowing just the value of the element, not it's place inside an array?
It needs to be cross-browser compatible too.
Thanks.
look to the example
arr = [1, 2, 3, 4, 5] // array
var removeItem = 2; // item to be removed
arr = jQuery.grep(arr, function(value) {
return value != removeItem;
});
// new array
// [1, 3, 4, 5]

Categories

Resources