So I am very new to coding and have run into an issue when it comes to comparing arrays, especially ones that are nested and trying to pull out values that match using their indices into a new array. Say I have two different arrays in the example where one is a nested array:
const array1 = [1,2,3,4,5,6,7,8,9,10]
const array2 =
[
[1,2,4,6]
[1,3,7,9,10]
[1,2]
[1,6,8,9]
]
I need the nested array to check each value against the original array to see if it matches and if it does then pull that value into a new array at the same index. IF it doesn’t match at that index then I need it to inert a delimiter or blank value of some kind then finish comparing the rest of the values against the two arrays. I also need the new outputted array to be the same length of the original array, so the expected output for the values above would be:
const ouputtedArray =
[
[1,2,,4,,6,,,,,]
[1,,3,,,,7,,9,10]
[1,2,,,,,,,,,]
[1,,,,,6,,8,9,]
]
I have tried a couple of different approaches and spent hours online looking for the answer with no hope. For example I have tried to compare the two arrays using a for loop:
for(let i = 0; i <=array1.length;i++){
if( i + array2[0][i] == i + array1[i]){
const match = i + array2[0][i]
const arrays =[]
console.log([match])
}}
//console.log output
['1']
['2']
The end goal of this entire process is to be able to input this data into a spreadsheet, so I need to keep the blanks to represent empty cells within CSV format, so eventually the overall data would be:
const array1 = [1,2,3,4,5,6,7,8,9,10] // These are the headers for the spreadsheet
const ouputtedArray = // these are the data cells
[
[1,2,,4,,6,,,,,]
[1,,3,,,,7,,9,10]
[1,2,,,,,,,,,]
[1,,,,,6,,8,9,]
]
and the spreadsheet itself would look like this
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| - | - | - | - | - | - | - | - | - | -- |
| 1 | 2 | | 4 | | 6 | | | | |
| 1 | | 3 | | | | 7 | | 9 | 10 |
| 1 | 2 | | | | | | | | |
| 1 | | | | | 6 | | 8 | 9 | |
I have no idea what to do here and am struggling to find a solution. Any help would be absolutely amazing!