javascript数组元素是否知道它们的封闭数组?

时间:2012-09-11 16:18:36

标签: javascript arrays

好的,我们有这个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"的元素,这是兄弟数组中的第二个元素。 请不要开枪!

Here is the fiddle

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

2 个答案:

答案 0 :(得分: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]));

答案 1 :(得分: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/