0

I would like some assistance to remove duplicates and remove the | and the '' at the start and end.

My code so far

const thedates = this.results
            .filter((result) => result.thedate)
            .map((item) => item.thedate)
            .filter((thedate, i, arr) => arr.indexOf(thedate) === i);
          // Split multiple thedates in strings and store in an array
          let thedate = [];
          thedates.forEach((item) => {
            const splitArr = item.split(", ");
            thedate = thedate.concat(splitArr).sort();
          });

          // Filter again for unique thedates
          this.thedates = thedate.filter(
            (thedate, i, arr) => arr.indexOf(thedate) === i
          );

My output in the console from the code above

'full-time', 'full-time|full-time', 'full-time|full-time|full-time', 'full-time|full-time|full-time|full-time', 'full-time|full-time|part-time|full-time|part-time|part-time',

I would just like each entry to say: full-time, part-time or full-time if there is just one between the quotes.

Can anyone help to add to my code please?

3
  • 2
    Can you provide a sample array with expected output? Commented Aug 15, 2022 at 15:27
  • 2
    Give sample input Commented Aug 15, 2022 at 15:28
  • This is the field in the JSON file which I'm pulling in. "thedate": "full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|part-time|full-time|full-time|part-time|full-time|part-time|full-time|full-time|part-time|part-time", The expected output is "thedate": "full-time, part-time", Commented Aug 15, 2022 at 15:53

3 Answers 3

2

You're essentially asking two things, how to turn a delimited string into array and how to remove duplicate values from an array. You can parse by using the .split() method, and remove duplicates from an array by constructing a set with it then turning it back into an array with the spread operator.

Altogether (where array is your input array):

let filteredArray = [ ...new Set( string.split( '|') ) ]

const string = "full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|part-time|full-time|full-time|part-time|full-time|part-time|full-time|full-time|part-time|part-time";
let filteredArray = [ ...new Set( string.split( '|') ) ]
let result = filteredArray.join(', ');
console.log(result)

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

2 Comments

added a demo and completed the answer with a join
Great, thank you! I will try this within my code too.
1

You could try something like this (similar to @Julien Mellon's post) where you use .split(), but you return an array of arrays with the second level array being the entry:

const thedates = ['full-time', 'full-time|part-time', 'full-time|part-time|full-time', 'full-time|full-time|part-time|full-time', 'full-time|full-time|part-time|full-time|part-time|part-time']

const theDatesFormatted = thedates.map(item => {  
  const arr = item.split('|')
  const uniqueArr = [...new Set(arr)]
  return uniqueArr
})

console.log(theDatesFormatted)

2 Comments

Thank you for taking the time to reply to my question. Your example is great but it doesnt remove the duplicates.
Hey @user13928344 of course no problem. I updated the code to remove duplicates. The [...new Set(arr)] part removes the duplicates.
0

if your inputs are

'full-time', 'full-time|full-time', 'full-time|full-time|full-time', 'full-time|full-time|full-time|full-time', 'full-time|full-time|part-time|full-time|part-time|part-time'

perhaps you could just call .split('|') ?

5 Comments

Please read the question carefully. And provide a practical code to cover all aspects. If you have any questions or suggestions, first ask them in the comments section.
I have made some progress but there is still an issue with my output that I dont understand.
console.log("the result1: " + modecode); the result1: full-time,full-time|full-time,full-time|full-time|full-time,full-time|full-time|full-time time|full-time|part-time|part-time,full-time|full-time| etc etc....
const string = modecode; //"full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|full-time|part-time|part-time|full-time|full-time|part-time|full-time|part-time|full-time|full-time|part-time|part-time"; let filteredArray = [...new Set(string.toString().split("|"))]; let result = filteredArray.join(", "); console.log("the result2: " + result); the result2: full-time,full-time, full-time, part-time, part-time,full-time, full-time,part-time,part-time, full-time,part-time, part-time,part-time
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.