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
Related
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
This question already has answers here:
Check whether an array exists in an array of arrays?
(11 answers)
Closed 3 years ago.
My problem is about comparing the values of two different arrays, not to know if they are equal, but if a value in array A exists in array B.
Includes doesn't work, and I don't compare the length, only the values.
I spend many hours looking for an answer but found nothing precisely about this problem.
firstArray = [0,1];
secondArray = [[0,1],[0,2],[0,3],[1,1],[1,2],[1,3]];
How can I can compare firstArray and secondArray, to know if secondArray has the value of firstArray. It is like an equal comparison but only if the value of firstArray is in secondArray.
If it is, then the player can move on the board. The idea is that as long as firstArray value is one of secondArray value, the player can move. If not, no move possible.
You can stringify array items and compare:
var firstArray = [0,1];
var secondArray = [[0,1],[0,2],[0,3],[1,1],[1,2],[1,3]];
var res = secondArray.some(x => JSON.stringify(x) == JSON.stringify(firstArray));
console.log(res);
You can use Array.prototype.some() combined with Array.prototype.toString():
const firstArray = [0,1];
const secondArray = [[0,1],[0,2],[0,3],[1,1],[1,2],[1,3]];
const isFound = secondArray.some(a => a.toString() === firstArray.toString());
console.log(isFound);
Simply use JSON.stringify with some and sort():
var firstInSecond = secondArray.some(e => JSON.stringify(e.sort()) == JSON.stringify(firstArray.sort()));
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);
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);
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;
}