How to compare two arrays with text strings and create a new one from repeated words in these

0

I have two arrays, one with a text with more words and the other with the text that I should compare, the problem is that there is a word that appears twice in each array so when I run the code it repeats itself in the new array, when it should appear according to the order of array to validate
This is my code:

    var taskInstructions = ["Fix a bug and something something remote", 
    "Must push the new version to the cloud"];
    var actualTask = "Fix a bug and push the new version to the remote";
    //como taskInstructions es un array lo cambio a string para despues 
    //volverlo a array, solo para que sea un solo elemento, hago lo mismo
    //con el otro array
    taskInstructions = taskInstructions.toString();
    taskInstructions = taskInstructions.replace(',', ' ');
    taskInstructions = taskInstructions.split(' ');
    actualTask =actualTask.split(' ');
    var jaredTask=[];
    for(i=0; i < actualTask.length; i++ ){
    for (n=0; n <= taskInstructions.length; n++){
    if(actualTask[i] == taskInstructions[n]){
    jaredTask.push(taskInstructions[n]);
    }
    }
    }
    //lo convierto otras vez para quitar las ','
    jaredTask =jaredTask.toString();
    var change = /,/g;
    jaredTask = jaredTask.replace(change , ' ');
    //el resultado muestra la palabra 'the' repetida de a 2 veces
    console.log(jaredTask);
    
asked by ecanro 17.04.2018 в 19:56
source

1 answer

1

If you want the elements to be unique, you can use this code:

if(actualTask[i] == taskInstructions[n]){
    if(jaredTask.indexOf(taskInstructions[n]) == -1) {
        jaredTask.push(taskInstructions[n]);
    }
}
    
answered by 17.04.2018 в 20:04