1

好的,我们有这个 sql 比较工具,它比较两个数据库并吐出 HTML 差异的报告。我需要解析它创建的 javascript 以了解有关差异的更多详细信息。例如,如果它显示 500 个更改的存储过程,我无法按更改的人或时间进行筛选。但是如果我可以解析 JS,我可以根据这些条件进行过滤。但是,它们的 JS 结构相当复杂,主要是嵌套数组,有时会低至 4 层。这是一个显示 4 条不同记录的示例。

var createsql  = new Array(

new Array(new Array(0, "Error, No SQL Availible")),

new Array (
  new Array(0,"-- Stored Procedure", " "),
  new Array(0,"", ""),
  new Array(1,"--------------------------------------------------------------------------------------", " "),
  new Array(1,"/*", " "),
  new Array(1,"Procedure        : Schema.p_procedure1", " "),
  new Array(1,"Description       : This process will do something", " "),
  new Array(1,"Creation Date  : 11/24/2009", " "),
  new Array(1,"Created By       : John Doe", " "),
  new Array(1,"Tables Updated : None", " "),
  new Array(1,"*/", " "),
  new Array(0, "", "")
),



new Array (  
  new Array(0,"-- Stored Procedure", " "),
  new Array(0,"", ""),
  new Array(1,"--------------------------------------------------------------------------------------", " "),
  new Array(1,"/*", " "),
  new Array(1,"Procedure        : Schema.p_procedure2", " "),
  new Array(1,"Description       : This process will do something", " "),
  new Array(1,"Creation Date  : 11/24/2011", " "),
  new Array(1,"Created By       : Jane Doe", " "),
  new Array(1,"Tables Updated : None", " "),
  new Array(1,"*/", " "),
  new Array(0, "", "")
),



new Array (
  new Array(0,"-- Stored Procedure", " "),
  new Array(0,"", ""),
  new Array(1,"--------------------------------------------------------------------------------------", " "),
  new Array(1,"/*", " "),
  new Array(1,"Procedure        : Schema.p_procedure3", " "),
  new Array(1,"Description       : This process will do something", " "),
  new Array(1,"Creation Date  : 11/24/2012", " "),
  new Array(1,"Created By       : Mark Doe", " "),
  new Array(1,"Tables Updated : None", " "),
  new Array(1,"*/", " "),
  new Array(0, "", "")
)


);

我可以解析数组并使用这段代码得到我需要的东西

var match = "Jane Doe";
var createLength = createsql.length;
var matched;

for(var i = 0; i < createLength; i++){
    if(typeof createsql[i] === "object"){
        var firstArrayLength = createsql[i].length;
            for(var j = 0; j < firstArrayLength; j++){
                if(typeof createsql[i][j] === "object"){
                    var secondArrayLength = createsql[i][j].length;
                        for(var k = 0; k < secondArrayLength ; k++){
                            if(typeof createsql[i][j][k] === 'string'){
                                if((createsql[i][j][k].indexOf(match) != -1)){
                                    console.log(createsql[i][j][k]);
                                    console.log('matched');
                                    //need to grab the parent and search its elemets for the one that starts with the word "Procedure"
                                }
                            }
                        } 
                }

            }    
    }
}

但我坚持的是如何获得我目前所在的数组的父级?例如,如果我的搜索字符串是Jane Doe,我想检索表示"Procedure : Schema.p_procedure2"同级数组中的第二个元素的元素。请不要拍我!

这是小提琴

PS:欢迎任何其他改进循环的建议,这只是一个示例,列表可能会更大。

4

2 回答 2

1

我可能遗漏了一些东西,但我认为答案相对简单:由于您知道数组结构,因此您可以使用createsql[i][4][1].

注意:如果你想动态搜索以“Procedure”开头的项目,你可以用更多的代码来做到这一点。我认为您的问题的关键答案是您刚开始createsql[i]从适当的父数组开始您的第二次搜索。

小提琴:http: //jsfiddle.net/nate/DBM7H/2/

console.log('Procedure', createsql[i][4][1]);

更新:

这是一个如何动态查找程序项的示例。

小提琴:http: //jsfiddle.net/nate/DBM7H/4/

创建一个函数来查找以“Procedure”开头的项目:

var findProcedure = function (record) {
    var i, length;
    for (i = 0, length = record.length; i < length; i += 1) {
        if (record[i][1].substring(0, 9) === 'Procedure') {
            return record[i][1];
        }
    }
}

然后只需在代码中使用该函数和正确的记录:

// Find the procedure
console.log(findProcedure(createsql[i]));
于 2012-09-11T16:25:47.680 回答
1

如果这些数组的形式是恒定的,我强烈建议将它们转换为原生 javascript 对象,以提高使用它们的简单性:

var sqlObjs = [];

for (var i = 0; i < createsql.length; i++){
    var item = createsql[i];     
    if (item.length != undefined && item.length > 1){
        sqlObjs.push({
            Procedure     : item[4][1].split(":")[1].trim(),
            Description   : item[5][1].split(":")[1].trim(), 
            CreationDate  : new Date(item[6][1].split(":")[1].trim()),
            CreatedBy     : item[7][1].split(":")[1].trim(),
            TablesUpdated : item[8][1].split(":")[1].trim(),
        });
    }
}

然后,您可以以更简洁的方式搜索数组:

var match = "Jane Doe";

for (var i = 0; i < sqlObjs.length; i++){
    var item = sqlObjs[i];

    if (item.CreatedBy == match){
        console.log(item.Procedure);
        break;
    }
}

你的生活会少一些。:)

你可以在这里看到这个工作:http: //jsfiddle.net/5fVqZ/1/

您可以实现一个简单的搜索来查找您要查找的项目的值,如下所示:

function getValue(byName, arry){
    var val = "";
    // this search assumes that the value will always be in the second position of the array
    for (var i = 0; i < arry.length; i++){
        var aVal = arry[i][1];

        if (aVal.indexOf(byName) == 0){ // only grab the value if byName is at the start of the string
            val = aVal.split(":")[1].trim();
            return val;
        }
    }
    return val;
}

像这样使用:

for (var i = 0; i < createsql.length; i++){
    var item = createsql[i];     
    if (item.length != undefined && item.length > 1){
        sqlObjs.push({
            Procedure     : getValue("Procedure", item),
            Description   : getValue("Description", item), 
            CreationDate  : new Date(getValue("Creation Date", item)),
            CreatedBy     : getValue("Created By", item),
            TablesUpdated : getValue("Tables Updated", item),
        });
    }
}

http://jsfiddle.net/5fVqZ/3/

于 2012-09-11T16:42:46.447 回答