I am trying to figure out a way to take a string and update values based on a new string that is coming in. The string represents the structure of a table and contains a column name and a column data type.
To do this I am trying to convert the original string and the new string into arrays. Then I need to union the results but update existing columns with their new data types.
For instance, in the below example I want to return 4 columns where col2 is updated from varchar(30 to varchar(20).
var original_schema = 'col1 int,col2 varchar(30),col3 datetime2,col5 bit';
var new_schema = 'col1 int,col2 varchar(20),col3 datetime2,col4 int';
var arr_original_schema = original_schema.replace(/ /g,':').split(",");
var arr_new_schema = new_schema.replace(/ /g,':').split(",");
console.log(arr_original_schema);
console.log(arr_new_schema);
function arrayUnique(array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
//removed
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
var uniqueschema = arrayUnique(arr_original_schema.concat(arr_new_schema));
console.log(uniqueschema)
The expected result is: col1 int,col2 varchar(20),col3 datetime2,col4 int,col5 bit"