简体   繁体   中英

Javascript How to update value in array based on matching value in different array

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"

var original_schema = 'col1 int,col2 varchar(30),col3 datetime2';
var new_schema = 'col1 int,col2 varchar(20),col3 datetime2,col4 int';


const arr_1 = original_schema.split(',');
const arr_0 = (new Array(arr_1.length)).fill(null);
let arr_2 = new_schema.split(',')
arr_2=Object.assign(arr_0, arr_2)

const result =  arr_2.filter(function (el) {
  return el != null;
}).join(',')

console.log(result);

I'd create objects of it, then use Object.assign

 const original_schema = 'col1 int,col2 varchar(30),col3 datetime2,col5 bit'; const new_schema = 'col1 int,col2 varchar(20),col3 datetime2,col4 int'; const objectify = (string) => string.split(',').reduce((a, b) => { const [col, type] = b.split(/\\s+/); a[col] = type; return a; }, {}); const os = objectify(original_schema); const ns = objectify(new_schema); const res = Object.entries(Object.assign(os, ns)).map(e => e.join(' ')).join(','); console.log(res);

 const original_schema = 'col1 int,col2 varchar(30),col3 datetime2,col5 bit'; const new_schema = 'col1 int,col2 varchar(20),col3 datetime2,col4 int'; const originalArr = original_schema.split(","); const newArr = new_schema.split(","); // Create single array with duplicates const mergedArray = [...originalArr, ...newArr]; // Run through merged array with duplicates and create/overwrite existing values let mergedObj = mergedArray.reduce((acc, item) => { let [id, str] = item.split(' '); acc[id] = item; return acc; }, {}); // sort the values of object using local compare // Assume that each value always start with col and number and they should be ordered as such. // use localCompare to compre these values. let sorted = Object.values(mergedObj).sort((a, b) => a.localeCompare(b)); console.log(sorted.join(','));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM