0

I have a function that have :
- Inputs : an array of Data

[
    {"position":"1","token":"Darién","tag":"PERSON"},
    {"position":"2","token":"Gap","tag":"PERSON"},
    {"position":"3","token":"make","tag":"O"},
] 

I iterate that array to get the token and tag and then based on a checkbox selection (person or country ...etc) I make the token string red and show it in a div element.

 function makeNamesRed(array) {

        let result = ''

        array.forEach((element, index, array) => {
            var word = element.token;
            var tag = element.tag;

            var checkedElements = new Array();
            $("input:checkbox[name=selectionFilter]:checked").each(function () {
                checkedElements.push($(this).val());
            });

            checkedElements.forEach((elementCheck, indexCheck, arrayCheck) => {

                if (word != "-LRB-" && word != "-RRB-") {

                    if (tag == elementCheck) {
                        word = "<span class='namePerson' "
                             + "style='color:red;' "
                             + "data-toggle='tooltip' "
                             + "data-placement='top' "
                             + "title='Name of a person'>" 
                             + word 
                             + "</span>";
                        result += word + " ";
                    }

                    else {
                        result += word + " ";
                    }
                }

            })
        })
        $("#editor").html(" ");
        $("#editor").html(result);
    }

The problem is that looping the selected checkbox array (checkedElements) here

else {
    result += word + " ";
} 

adds the words (that are not found) every time again. like so : Darién make make make. Number of repetition in make depends on how many checkboxes are selected.

Do you have an idea how to solve that and make that better ?

EDIT: Here is a Jsfiddle of that scenario :

Scenario

7
  • Was (word != "-LRB-" & word != "-RRB-") intentional or did you mean (word != "-LRB-" && word != "-RRB-")? Commented Nov 28, 2018 at 7:53
  • 2
    Also, in both the .forEach() functions, you are reusing the same variables (element, index, array). I think you should make them unique. Commented Nov 28, 2018 at 7:56
  • @LloydFrancis thanks for catching that, I've meant && . also +1 for variable names. Commented Nov 28, 2018 at 9:10
  • did it solve your problem? Commented Nov 28, 2018 at 10:03
  • 1
    Can you provide some sample values that might be inside the array checkedElements after user interaction from the HTML? Commented Nov 28, 2018 at 10:25

1 Answer 1

1

If I understand your question correctly then the issue is your inner loop. You don't need to check every single item in checkedElements against every single item in array - you just need to check if each item in array has a tag property that matches a value found in checkedElements (use Array.prototype.indexOf to achieve this.)

Below is some code that I think solves your issue (I left out your specific conditional logic but you can add that).

var checkedElements = ["PERSON", "LOCATION", "COUNTRY"];
const spanTemplate = `<span class='namePerson' style='color: red;' data-toggle='tooltip' data-placement='top' title='Name of a person'>{TOKEN}</span>`;

function makeNamesRed(array) {
  let result = '';
  array.forEach(el => {
    if (checkedElements.indexOf(el.tag) > -1) {
      result += spanTemplate.replace(/{TOKEN}/gi, el.token);
    }
  });
  return result;
}

console.log(makeNamesRed([{
    position: 1,
    token: "Darién",
    tag: "PERSON"
  },
  {
    position: 2,
    token: "Gap",
    tag: "PERSON"
  },
  {
    position: 3,
    token: "make",
    tag: "O"
  }
]));

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

Comments

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.