var array1 = ['a','b','c','d'];
var array2 = ['a','v','n','d','i','f'];
var array3 = ['1','2','3','4','5','6'];
Just starting to learn Javascript, I can't figure out how to compare the values of array2
to those on array1
and if so replace it with the corresponded array index from array3
.
To turn it like this:
array2 = ['1','v','n','4','i','f'];
But also, it has to compare the values from array1 and array2 even if the index positions are different like this:
var array1 = ['a','b','c','d'];
var array2 = ['d','v','n','a','i','f'];
Thanks for the help
Use Array#map
You can use
map()
on array2 and see if current element is same as element in array1 with same index if it is return element from array3 with index ofi
else return current element ore
You could use a hash table and check against. If the string is not included in the hash table, a replacement value is set for this element.
ES6 with
Set
there are many forms, this is a basic form:
Here you have some code that does what you ask:
Hope it helps!