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

166
Views
Create a delimited string as per different condition

I am working on below snippet :

let prevFilterName = ''
let filterCourse = ''
let filterIds = ['1','2','3','4','5','6','7','8','9']
let filterDetails = [
    {
    id: 1,
    Name: 'David',
    Course: 'English'
    },
    {
    id: 2,
    Name: 'Peter',
    Course: 'German'
    },
    {
    id: 3,
    Name: 'Peter',
    Course: 'React'
    },
    {
    id: 4,
    Name: 'John',
    Course: 'English'
    },
    {
    id: 5,
    Name: 'John',
    Course: 'NodeJs'
    },
    {
    id: 6,
    Name: 'John',
    Course: 'Docker'
    },
    {
    id: 7,
    Name: 'Andrew',
    Course: 'Dutch'
    },
    {
    id: 8,
    Name: 'Andrew',
    Course: 'Angular'
    },
    {
    id: 9,
    Name: 'David',
    Course: 'SQL'
    }
]

filterIds.forEach((id) => {
    let FindFilter = filterDetails.find((filterItem) => {return filterItem.id == id})
    console.log(FindFilter);
    if (prevFilterName == FindFilter.Name){
        filterCourse = filterCourse + ',' + FindFilter.Course
    }
    else if (prevFilterName != FindFilter.Name){
        filterCourse = filterCourse + FindFilter.Course + ' > '
    }

    prevFilterName = FindFilter.Name
    console.log(filterCourse)
})

Output which I am expecting is :

English > German,React > English,NodeJs,Docker > Dutch,Angular,SQL

But I am getting output as below :

English > German > ,ReactEnglish > ,NodeJs,DockerDutch > ,AngularSQL >

Can someone help here to get it right?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When a difference is detected, you then do

filterCourse = filterCourse + FindFilter.Course + ' > '

You're putting the > in the wrong place - it needs to come between the previous element and the current one, not after both.

let prevFilterName="",filterCourse="",filterIds=["1","2","3","4","5","6","7","8","9"],filterDetails=[{id:1,Name:"David",Course:"English"},{id:2,Name:"Peter",Course:"German"},{id:3,Name:"Peter",Course:"React"},{id:4,Name:"John",Course:"English"},{id:5,Name:"John",Course:"NodeJs"},{id:6,Name:"John",Course:"Docker"},{id:7,Name:"Andrew",Course:"Dutch"},{id:8,Name:"Andrew",Course:"Angular"},{id:9,Name:"David",Course:"SQL"}];

filterIds.forEach((id) => {
  let FindFilter = filterDetails.find((filterItem) => {
    return filterItem.id == id
  })
  if (prevFilterName == FindFilter.Name) {
    filterCourse = filterCourse + ',' + FindFilter.Course
  } else if (prevFilterName && prevFilterName != FindFilter.Name) {
    filterCourse = filterCourse + ' > ' +FindFilter.Course;
  } else {
    // initial
    filterCourse = FindFilter.Course;
  }

  prevFilterName = FindFilter.Name
})

console.log(filterCourse)

Another method, which I'd prefer - group the objects first, then join by the delimiter needed (, or >).

const filterDetails=[{id:1,Name:"David",Course:"English"},{id:2,Name:"Peter",Course:"German"},{id:3,Name:"Peter",Course:"React"},{id:4,Name:"John",Course:"English"},{id:5,Name:"John",Course:"NodeJs"},{id:6,Name:"John",Course:"Docker"},{id:7,Name:"Andrew",Course:"Dutch"},{id:8,Name:"Andrew",Course:"Angular"},{id:9,Name:"David",Course:"SQL"}];


let previousName;
const grouped = [];
for (const obj of filterDetails) {
  if (obj.Name !== previousName) {
    grouped.push([]);
  }
  grouped[grouped.length - 1].push(obj.Course);
  previousName = obj.Name;
}
const result = grouped
  .map(courses => courses.join(','))
  .join(' > ');
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!