1

I'm trying to remove duplicates array's items (which are nested array's items) by comparing each of the second value (the date nested item, eg: From 22/07/21 10:00 to 22/07/21 12:00).

I'm struggling with it because the third nested item's value is a random string of character. Any help is appreciated.

Initial array:

[
  ['F-GMXB', 'From 22/07/21 10:00 to 22/07/21 12:00', 'M8202042500022072100000007'],
  ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M8802052500023072100000004'],
  ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M28681025000260C2100148836'],
  ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M2868102500026072100128886'],
]

Expected result, new array:

[
  ['F-GMXB', 'From 22/07/21 10:00 to 22/07/21 12:00', 'M8202042500022072100000007'],
  ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M8802052500023072100000004'],
]

2 Answers 2

1

You can store the previous dates in an array (a) and use Array#filter to check whether it includes it or not:

If it doesn't, add the date to the array

const arr = [['F-GMXB', 'From 22/07/21 10:00 to 22/07/21 12:00', 'M8202042500022072100000007'],['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M8802052500023072100000004'],['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M28681025000260C2100148836'],['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M2868102500026072100128886']];

var a = [], res = arr.filter(e => !a.includes(e[1]) ? a.push(e[1]) : false);

console.log(res);

Sign up to request clarification or add additional context in comments.

1 Comment

Oh i see. My idea at firs was to get the position of each duplicated date and remove each key based on that. But your way is really ingenious! Thanks a lot for the help!
0

You can also do that

const arr1 =
[ ['F-GMXB', 'From 22/07/21 10:00 to 22/07/21 12:00', 'M8202042500022072100000007']
, ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M8802052500023072100000004']
, ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M28681025000260C2100148836']
, ['F-HMRT', 'From 26/07/21 07:00 to 27/07/21 22:00', 'M2868102500026072100128886']
]
const arr2 =  arr1.filter(( c,i,{[i-1]:p})=>( !i || c[1]!=p[1])  )
 

console.log(arr2)
.as-console-wrapper {max-height: 100%!important;top:0 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.