I have 3 arrays
array1 = [a,b,c,d,e,f]
array2 = [a,b,d,f]
array3 = [g,h]
How could I figure out where in array2 doesn't match array1 and then insert an element from array3?
So the output would look like
array4 = [a,b,g,d,h,f]
var EnglishArray = [<p>,This is a longer description. This will describe the item in greater detail.,</p>,<span please no translated Large donkey>,cat,</span>,<div>,test,<large>,<idk>,•,Hello worlds,<end?>]
var HTMLTags = [<p>,</p>,<span please no translated Large donkey>,</span>,<div>,<large>,<idk>,•,<end?>]
var translations = [Esta es una descripción más larga. Esto describe el tema con mayor detalle.,gato,prueba,Hola mundos]
function newString(EnglishArray, HTMLTags, translations){
var array4 = EnglishArray;
var j = 0;
for (var i = 0; i < EnglishArray.length; i++) {
if(HTMLTags[i] != EnglishArray[i]){
array4.splice(i, 1, translations[j]);
j++;
i++;
}
}
newString = array4.join('');
return newString;
}
The output it is giving me is
<p>Esta es una descripción más larga. Esto describe el tema con mayor detalle.</p>gatocatprueba<div>Hola mundos<large>•<end?>
Instead of
<p>Esta es una descripción más larga. Esto describe el tema con mayor detalle.</p><span please no translated Large donkey>gato</span><div>prueba<large><idk>•Hola mundos<end?>
When I try this on a lot smaller scale it works fine. But it seems the more tags, and translations there is more errors. After it gets too long it will start repeating the last couple words in English.
You could check if the value is included array2
and if not take an element of array3
.
var array1 = ['a', 'b', 'c', 'd', 'e', 'f'],
array2 = ['a', 'b', 'd', 'f'],
array3 = ['g', 'h'],
result = array1.map(v => array2.includes(v) ? v : array3.shift());
console.log(result);
Without map
and includes
var array1 = ['a', 'b', 'c', 'd', 'e', 'f'],
array2 = ['a', 'b', 'd', 'f'],
array3 = ['g', 'h'],
result = [],
i,
l = array1.length;
for (i = 0; i < l; i++) {
result.push(array2.indexOf(array1[i]) === -1 ? array3.shift() : array1[i]);
}
console.log(result);