Here is one "get you started" approach:
You describe your data, but you do not provide any concrete samples - so here is my starting data, making some assumptions based on the information in the question:
var arrayBig = [ [1, "A", "B"], [2, "C", "D"], [3, "E", "F"], [4, "G", "H"] ];
var arraySmall = [ [1, "P", "Q"], [3, "Y", "Z"] ];
With the above data, the expected outcome is the following, where rows 1 and 3 are replaced:
[ [1, "P", "Q"], [2, "C", "D"], [3, "Y", "Z"], [4, "G", "H"] ];
Here is the approach:
var arrayBig = [ [1, "A", "B"], [2, "C", "D"], [3, "E", "F"], [4, "G", "H"] ];
var arraySmall = [ [1, "P", "Q"], [3, "Y", "Z"] ];
var mapper = new Map();
arraySmall.forEach((row) => {
mapper.set(row[0], row);
} );
newArrayBig = [];
arrayBig.forEach((row) => {
if ( mapper.has( row[0] ) ) {
newArrayBig.push( mapper.get( row[0] ) );
} else {
newArrayBig.push( row );
}
} );
console.log( newArrayBig );
This assumes you have an iron-clad guarantee that there are never more rows of data in arraySmall
than there are in arrayBig
. You can (and should) add some logic to test for that, for more robust code.
Notes
The Map
object (not the map()
method) is central to this approach. This object provides a lookup structure: an index value, pointing to a data value:
var mapper = new Map();
arraySmall.forEach((row) => {
mapper.set(row[0], row);
} );
In our case, the index is the number in the first cell of each arraySmall
row. And the value it points to is the complete data array for that row, for example:
1 -> [1, "P", "Q"]
3 -> [3, "Y", "Z"]
We can now use this lookup data as we iterate through each row of our arrayBig
data:
arrayBig.forEach((row) => { ... } );
The logic inside this forEach
iterator basically says: If the map contains an array using the same number as the current arrayBig
row, then use the arraysmall
data. Otherwise, use the arrayBig
data.
The newArrayBig.push()
method adds that array to our results array.
You can then write this back to your spreadsheet, as needed.