The problem
I am iterating over a states array the looks like this
let states = [
['AL', 0],
['AK', 0],
['AZ', 0],
['AR', 0],
['CA', 0]......
]
and comparing it to states array that actually has values after the state code. I have a function that is trying to merge both array by checking the first array and seeing if the key (state) exists in the second array. If it does it should replace that array with the array found in the second array.
I've tried
Below is my attempt, it clearly does not work, I have tried shifting the array index and getting just the state code, but that throws an undefined error.
function mergeArrays(arr1, arr2) {
for(let i = 0; i < arr1.length; i++) {
let arr1state = arr1[i];
let arr2state = arr2[i];
if (arr1state === arr2state) {
console.log(arr1[i], arr2[i])
}
}
}
arr2is[['AZ', 1]]you want to change the third element ofstatesto['AZ', 1]...? Like this? jsfiddle.net/zmr0tyea