Home > Blockchain >  Create a delimited string as per different condition
Create a delimited string as per different condition

Time:10-12

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?

CodePudding user response:

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

  • Related