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!
3
Answers
Assuming
You should be able to use the following:
It sounds like you’re trying to compare the values in a nested array against a main array and insert matching values into new arrays, while leaving blanks for missing values. This can be done by iterating over the array2 nested arrays and comparing them against array1, inserting values into a new array when there’s a match and inserting a blank or delimiter when there isn’t.
Here’s a step-by-step approach and the code to achieve this:
Steps:
index), insert that value at the correct index in the new array.
or ”).
Once you have this array, converting it to a CSV format for your spreadsheet can be done easily by joining each sub-array into a comma-separated string:
OUTPUT
This will provide the output you need in CSV format, with empty values where there are no matches, making it ready to insert into a spreadsheet.
Customization:
main array. This method works as long as the values are primitives
(like numbers or strings). If you’re dealing with objects, you’d
need a custom comparison function.
Decomposing a little, we want a clone of
array1
substituting missing values with something like empty string, then we want to apply that to every subarray inarray2
That’s it.