Skip to content
Advertisement

Filter Array Javascript/typescript

I have an array that needs to be filtered with specific condition

JavaScript
  • If any value has -backup has suffix then it should take that and it should avoid the one without backup . Here fghij-backup has value with backup so that is the priority.

  • If the value doesnt have backup has suffix then it should take it has priority eg:qrstuv

I am struck with the second point . I have tried with this code

JavaScript

Advertisement

Answer

As per the explaination, the requirement is to filter out the words with -backup as suffix.

In case the words don’t have the -backup suffix, allow those words only when there is no alternative backup word present. E.g.: For abcde, since we already have abcde-backup, don’t allow it in result. But, for qrstuv, there is no backup alternative (qrstuv-backup), so allow it.

So, the code for this would be as follow:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement