This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 2 years ago.
I have two arrays below,
let array_one = [1, 2, 3, 4, 5]; //first array
let array_two = [5, 6, 7, 8, 9]; //second array
//I need [1, 2, 3, 4, 5, 6, 7, 8, 9]
In array_one and array_two has one common element that is 5 but I need this 5 only one time. I don't need to remove the duplicate elements but need one single element.
Please try this, It should work for you
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
let arr = [...array_one,...array_two];
arr.filter(onlyUnique);
Related
This question already has answers here:
All possible combinations of a 2d array in Javascript
(2 answers)
Closed 1 year ago.
I want to transform one array of arrays by doing a specific transform, like the following example:
[[1, 2, 3], [4, 5]] => [14, 15, 24, 25, 34, 35];
Note, the first element of the first array is concatenated to each element of the second array to form a new array, and so on with the second element, third... etc
Use array.concat and for loop
let arrays = [
["a1","b1","c1"],
["a2","b2","c2"],
["a1","b1","c1"],
["a1","b1","c1"],
]
let arr = [];
arrays.forEach(array =>{
arr = arr.concat(array)
})
console.log(arr)
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
If I have an array A = [1, 2, 3, 4, 5] and B = [3, 4, 5] I want to return a new array with values
[1, 2]. remove same value.
Using Array.prototype.includes, you can check if B contains A item or not.
And using Array.prototype.filter, you can get the filtered values which are not included in array B.
const A = [1, 2, 3, 4, 5];
const B = [3, 4, 5];
const output = A.filter((item) => !B.includes(item));
console.log(output);
This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 2 years ago.
How can I delete all the items from a JavaScript array?
example -
var myArray1, 2, 3, 4, 5, 6, 7, 8, 9];
// I want it to become this
myArray = [];
You can try this:
function clearArray(array) {
while (array.length) {
array.pop();
}
}
This question already has answers here:
Counting the occurrences / frequency of array elements
(39 answers)
Closed 3 years ago.
Given an array of elements e.g. [9, 9, 9, 2, 5], how can I create a map of element and their frequency in the array [9=>3, 2=>1, 5=>1] using higher order function of array such as map, filter and reduce.
The idea is same as other answer but it doesn't print right map so I am posting correct one.
let arr = [9, 9, 9, 2, 5];
let res = arr.reduce((acc, cur) => (!acc[cur] ? acc[cur] = 1 : acc[cur]++, acc), {})
console.log(res)
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(141 answers)
Closed 7 years ago.
I have an array in javascript.Example
var array=[1,2,3,4,5,2,6,7,2];
Ok, i want to delete value 2 in array. The result as
var array=[1,3,4,5,6,7];
Thank guys.
Use filter() for that
var array = [1, 2, 3, 4, 5, 2, 6, 7, 2];
array = array.filter(function(v) {
return v != 2
});
console.log(array);