Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Filter Array Javascript/typescript

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

**Input**
    let arr=[
        "abcde-backup",
        "abcde",
        "fghij-backup",
        "fghij",
        "klmnop-backup",
        "qrstuv"
    ]
    I am trying to achieve output like this
**output**
    [
        "abcde-backup",
        "fghij-backup",
        "klmnop-backup",
        "qrstuv"
    ]
  • 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

function getLatestJobId(){ 
    let finalArr=[];
    arr.forEach(val=>{
      if(val.indexOf('-backup') > -1){
         finalArr.push(val);
      }   
    })
    
    return finalArr;
} 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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:

let arr=[
        "abcde-backup",
        "abcde",
        "fghij-backup",
        "fghij",
        "klmnop-backup",
        "qrstuv"
    ]

function getLatestJobId(){ 
    return arr.filter(val => val.endsWith('-backup') || !arr.includes(`${val}-backup`))
}

console.log(getLatestJobId())

about 4 years ago · Juan Pablo Isaza Report

0

You can do:

const arr = [
  'abcde-backup', 
  'abcde', 
  'fghij-backup', 
  'fghij', 
  'klmnop-backup', 
  'qrstuv'
] 

const result = Object.values(arr.reduce((a, c) => {
  const [w] = c.split('-')
  a[w] = a[w]?.endsWith('-backup')
    ? a[w]
    : c
  return a
}, {}))

console.log(result)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!