Info: My problem is already solved. The answer from @kennarddh broke my mental block. Please go on if you are still interested.
Consider the following array:
const data = [
['A', 'B', 'C', ''],
['D', 'E', 'F', 'G']
]
How would you merge each column of each row together into a new array with just one row using newline character?
Expected output:
const result = [
['A\nD', 'B\nE', 'C\nF', '\nG']
]
For a better understanding of what this is all about. I have several excel files with my data in it. Most of them are formatted equal but some are not. To find my data in a file if it is not formatted as expected. I use fuzzy search to find the relevant cells on a sheet. But Fuse.js expects string lists to search in it. Therefore I have to convert the cell structure into a one dimensional list of strings. Because humans are lazy some data headers are stretching across two or more rows. Especially if some headers describe similar data like "price with vat" or "price without vat":
const fileData = [
[],
['some', 'irrelevant', 'data']
[''],
['', '', 'vat'], // <-- start of headers
['product', 'price', 'price'], // <-- end of headers
['data', 'data', 'data'], // <-- start of data
['data', 'data', 'data'], // <-- end of data
[''],
]
In cases like this searching only for the header:
'price'
would return two results. Which could not be differentiated from each other.
Merging the two or more header rows together using newlines instead:
const merged = [
['\nproduct', '\nprice', 'vat\nprice']
]
Would give a unique list of headers which could be differentiated. The fileData needs to be packed accordingly to be searched.
Given the relevant data headers from a config file I prepared. I could than search for:
'\nprice'
or
'vat\nprice'
to get the column position of that header. Now I have a pretty good assumption that I'm importing the correct data from the correct position in a file.