So I have two arrays looking like this :
arr 1 :
[[Tue Feb 20 09:00:00 GMT+01:00 2018, xxx, cc0902be495c4350a6bfcd1734c843b9, xxx, affiliate, 101723.0, ru, 9e09ee193e21766b1946e485eec9adcf, 0.81, 0.72, 6.05, 0.5265, 0.1053, 0.6318, 3.9325, 0.7865, 4.719, 0.468, 0.0936, 0.5616], [Tue Feb 21 09:00:00 GMT+01:00 2018, xxx, f8875453e5354d88931e3474021f723a, xxx, affiliate, 101723.0, ru, b4cb6e13bc1909b6f04f8cd44b1374d5, 0.5, 0.44, 3.72, 0.325, 0.065, 0.39, 2.418, 0.4836, 2.9016, 0.286, 0.0572, 0.3432],[Tue Feb 22 09:00:00 GMT+01:00 2018, xxx, f8875453e5354d88931e3474021f723a, xxx, affiliate, 101723.0, ru, b4cb6e13bc1909b6f04f8cd44b1374d5, 0.5, 0.44, 3.72, 0.325, 0.065, 0.39, 2.418, 0.4836, 2.9016, 0.286, 0.0572, 0.3432]]
arr 2 :
[[Tue Feb 20 09:00:00 GMT+01:00 2018, xxx, cc0902be495c4350a6bfcd1734c843b9, xxx, affiliate, 101723.0, ru, 9e09ee193e21766b1946e485eec9adcf, 0.81, 0.72, 6.05, 0.5265, 0.1053, 0.6318, 3.9325, 0.7865, 4.719, 0.468, 0.0936, 0.5616], [Tue Feb 21 09:00:00 GMT+01:00 2018, xxx, f8875453e5354d88931e3474021f723a, xxx, affiliate, 101723.0, ru, b4cb6e13bc1909b6f04f8cd44b1374d5, 0.5, 0.44, 3.72, 0.325, 0.065, 0.39, 2.418, 0.4836, 2.9016, 0.286, 0.0572, 0.3432],[Tue Feb 22 09:00:00 GMT+01:00 2018, xxx, f8875453e5354d88931e3474021f723a, xxx, affiliate, 101723.0, ru, b4cb6e13bc1909b6f04f8cd44b1374d5, 0.5, 0.44, 3.72, 0.325, 0.065, 0.39, 2.418, 0.4836, 2.9016, 0.286, 0.0572, 0.3432],[Tue Feb 23 09:00:00 GMT+01:00 2018, xxx, f8875453e5354d88931e3474021f723a, xxx, affiliate, 101723.0, ru, b4cb6e13bc1909b6f04f8cd44b1374d5, 0.5, 0.44, 3.72, 0.325, 0.065, 0.39, 2.418, 0.4836, 2.9016, 0.286, 0.0572, 0.3432]]
What I want to achieve, but don't really know how, is to remove the entries in arr 1 that have the same dates as those in arr 2 . Sp, considering the data presented above, all entries in arr will need to be removed, because their dates overlap with the entrie's dates in arr 2 .
How can I do this? The entry number or other values shouldn't matter. If, eg, in arr 1 I have 10k entries with date 5th of march and in arr 2 I have one single entry with the same date, I still want these 10k entries in arr 1 removed.
I tried to do this with filters, but since it's a 2d array I don't think this was the correct approach at all.
Use map
and filter
.
Create a array of all dates in arr2
var allArr2Dates = arr2.map( s => s[0] );
And then filter arr1
using this array
arr1 = arr1.filter( s => allArr2Dates.includes( s[0] ) );
ES5 equivalent
var allArr2Dates = arr2.map( function(s){ return s[0]; } );
arr1 = arr1.filter( function(s) { return allArr2Dates.includes( s[0] ); } );
It is possible, mixed filter and every.
let one = [[1, 'aaa'], [2, 'bbb'], [3, '444']]; let second = [[14, 'aaa'], [2, 'bbb'], [6, 'ccc']]; const result = one.filter(data => second.every(dataAux => dataAux[0] !== data[0]));
Map
can be leveraged to overwrite related elements
in key
based Arrays
.
See below for a practical example.
// Input. const array1 = [['Tue Feb 20 09:00:00 GMT+01:00 2018', 'A1'], ['Tue Feb 21 09:00:00 GMT+01:00 2018', 'A1'],['Tue Feb 22 09:00:00 GMT+01:00 2018', 'A1']] const array2 = [['Tue Feb 20 09:00:00 GMT+01:00 2018', 'A2'], ['Tue Feb 21 09:00:00 GMT+01:00 2018', 'A2'],['Tue Feb 22 09:00:00 GMT+01:00 2018', 'A2'],['Tue Feb 23 09:00:00 GMT+01:00 2018', 'A2']] // Output. const output = [...new Map([...array1, ...array2].map(day => [day[0], day])).values()] // Log. console.log(output)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.