Home > Enterprise >  Javascript prevent redundancy when joining multiple arrays
Javascript prevent redundancy when joining multiple arrays

Time:01-20

I have a function that creates a long link. It does this by letting people click on multiple answers that belong to multiple question. Each question with it's answers belong to part of a multidimentional array.

Link creator:

        /**
         * Returns the parameters for the URL.
         *
         * @returns {string}
         */
        getLink() {
            let quizUrl = control.url[control.questionNumber];
            this.tmp = [];
            for (let i = 0; i < this.url.length; i  ) {
                // Check if question is from the same quiz part and adds a , between chosen answers and add the right prefix at the beginning
                if (this.url[i].length > 0) {
                    this.tmp.push(""   Quiz[i].prefix   this.url[i].join(","))
                    // console.log(this.url)
                }
                    if (this.url[i].length === 0) {
                        this.tmp.push("");
                }
                // console.log(quizUrl.length)
                console.log(this.tmp);
            }
            /// If answers are from different quiz parts add a & between answers.
            return ""   this.tmp.join("&");
        };

Array:

    class QuizPart {
        constructor(questionText, chosenAnswer, prefix, questionDescription) {
            this.questionText = questionText;
            this.chosenAnswer = chosenAnswer;
            this.prefix = prefix;
            this.questionDescription = questionDescription;
        }
    }

    class ChosenAnswer {
        constructor(id, name) {
            this.id = id;
            this.name = name;
        }
    }

    let Quiz = [
        new QuizPart('Whats your size?', [
                new ChosenAnswer('6595', '41'),
                new ChosenAnswer('6598', '42'),
                new ChosenAnswer('6601', '43'),
                new ChosenAnswer('', ''),
            ], 'bd_shoe_size_ids=',
            'The size of your shoes is very important. If you have the wrong size, they wont fit.'),

        new QuizPart('What color would you like?', [
                new ChosenAnswer('6053', 'Red'),
                new ChosenAnswer('6044', 'Blue'),
                new ChosenAnswer('6056', 'Yellow'),
                new ChosenAnswer('6048', 'Green'),
                new ChosenAnswer('', ''),
            ], 'color_ids=',
            'Color isn t that important, It looks good tho.'),

        new QuizPart('What brand would you like?', [
                new ChosenAnswer('5805', 'Adidas'),
                new ChosenAnswer('5866', 'Nike'),
                new ChosenAnswer('5875', 'Puma'),
                new ChosenAnswer('', ''),
            ], 'manufacturer_ids=',
            'Brand is less important. Its just your own preference'),
    ]

Everytime you go over to a new question in getLink() it joins the 2 questions with an &. When you console log getLink() it looks like this:

bd_shoe_size_ids=6595&color_ids=6044&manufacturer_ids=5875,5866

But uif you skip a question it adds the & sign anyway. So if is skip the first question there's an & sign at the beginning. If i skip the second it adds 2 in the middel like this:

bd_shoe_size_ids=6595&&manufacturer_ids=5875

And if you skip the last one it adds on at the end.

I tried using replace to replace two & signs with only one and delete them if they are at the very beginning or end, but it doesn't work. Does anybody know a good way to not join them if no value is in the string?

CodePudding user response:

replace only works on the first found item in the string. You can either use replaceAll, or split and join, which is more cross-browser friendly as below:

function cleanLink(link) {
  let newLink = link.split('&&').join('&'); // remove duplicate ampersands
  if (newLink[0] === '&') {
    newLink = newLink.slice(1); // remove starting ampersand
  }
  if (newLink[newLink.length - 1] === '&') {
    newLink = newLink.slice(0,-1); // remove trailing ampersand
  }
  return newLink;
}
  •  Tags:  
  • Related