好的,我们有这个 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:欢迎任何其他改进循环的建议,这只是一个示例,列表可能会更大。