3

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, e.g, 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.

1
  • you perform check based on date only ?? or other fields also ? Commented Mar 13, 2018 at 12:06

3 Answers 3

4

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] ); } ); 
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for the reply. Actually i forgot to mention that I was doing it under ECMASCRIPT 5. Would you mind writing the EC5 version as well?
thanks for the edit! i am getting this error : TypeError: Cannot find function includes in object Tue Feb 20 2018 09:00:00 GMT+0100 (CET) Do you know why is that?
Use indexOf instead.
1

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

Comments

1

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.