无法枚举(动态)JavaScript对象

时间:2016-09-02 07:43:31

标签: javascript arrays javascript-objects

我正在处理包含执行X查询结果的JS对象数组。

每个查询结果都表示为嵌套的对象数组。

所述数组可能只包含一个成员(返回一行)或许多成员。每个成员/行可以包含单个字段/属性,或许多。

这是一个我已经JSON.stringify-ed的例子(请原谅我通过直接从控制台复制引入的一些转义字符):

 [
  [
    {
      "current_queries_running": "0",
      "timestamp": "2016-09-02 06:51:49",
      "guid": "8d9470cc-669b-97a2-8fd6-7bab0d0c052e"
    }
  ],
  [
    {
      "lasthour_queries_spilled_to_disk_count": "0",
      "timestamp": "2016-09-02 06:51:49",
      "guid": "d15bd886-e72c-9879-d997-f2d48af4afa6\r\n"
    }
  ],
  [
    {
      "database_name": "tpchdslitev5",
      "query_count": "56",
      "query_text": "SELECT count(distinct query) LastHour_queries_spilled_to_disk_count FROM svl_que",
      "min_execution_time": "0.00",
      "max_execution_time": "16.00",
      "average_execution_time": "0.55",
      "total_execution_time": "31.00",
      "max_query_id": 1958,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_\r\ncount": "51",
      "query_text": "select trim(database) as database_name, count(query) as query_count, max(substri",
      "min_executi\r\non_time": "0.00",
      "max_execution_time": "10.00",
      "average_execution_time": "0.29",
      "total_execution_time": "15.00",
      "max_query_i\r\nd": 1955,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "03\r\n8e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "9",
      "query_text": "select count(*) as\r\ncurrent_transaction_count from svv_transactions t WHERE t.loc",
      "min_execution_time": "0.00",
      "max_execution_time": "5.00",
      "\r\naverage_execution_time": "1.00",
      "total_execution_time": "9.00",
      "max_query_id": 1598,
      "last_run_date": "2016-09-01T16:00:00.00\r\n0Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database\r\n_name": "tpchdslitev5",
      "query_count": "9",
      "query_text": "select case when count(*) > 0 then 1 else 0 end as current_is_vacu\r\num_in_process ",
      "min_execution_time": "0.00",
      "max_execution_time": "6.00",
      "average_execution_time": "0.88",
      "total_execution\r\n_time": "8.00",
      "max_query_id": 1600,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-\r\n09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "9",
      "query_\r\ntext": "select count(distinct l.query) as LastHour_perf_alert_count from stl_alert_event",
      "min_execution_time": "0.00",
      "ma\r\nx_execution_time": "4.00",
      "average_execution_time": "0.77",
      "total_execution_time": "7.00",
      "max_query_id": 1602,
      "last_run_dat\r\ne": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4\r\n-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "9",
      "query_text": "select sum(total) as LastHour_internal_b\r\nroadcasting from (select count(query) to",
      "min_execution_time": "0.00",
      "max_execution_time": "4.00",
      "average_execution_tim\r\ne": "0.66",
      "total_execution_time": "6.00",
      "max_query_id": 1601,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "even\r\nt": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "9",
      "query_text": "select count(*) as current_ungranted_locks_on_resources from svv_transactions t ",
      "min_\r\nexecution_time": "0.00",
      "max_execution_time": "4.00",
      "average_execution_time": "0.55",
      "total_execution_time": "5.00",
      "max_qu\r\nery_id": 1599,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid\r\n": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "9",
      "query_text": "select avg(dat\r\nediff(ms, starttime, endtime)) as lastHour_QueryExecutionTime_sec ",
      "min_execution_time": "0.00",
      "max_execution_time": "5.\r\n00",
      "average_execution_time": "0.55",
      "total_execution_time": "5.00",
      "max_query_id": 1595,
      "last_run_date": "2016-09-01T16:00:\r\n00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "dat\r\nabase_name": "tpchdslitev5",
      "query_count": "9",
      "query_text": "SELECT SUM(w.total_queue_time) / 1000000.0 as LastHour_WLM_Qu\r\neueTime FROM stl_wl",
      "min_execution_time": "0.00",
      "max_execution_time": "3.00",
      "average_execution_time": "0.44",
      "total_exec\r\nution_time": "4.00",
      "max_query_id": 1597,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "\r\n2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "9",
      "q\r\nuery_text": "select avg(datediff(ms, starttime, endtime)) as lastHour_UserQueryExecutionTime_",
      "min_execution_time": "0.00\r\n",
      "max_execution_time": "2.00",
      "average_execution_time": "0.22",
      "total_execution_time": "2.00",
      "max_query_id": 1596,
      "last_ru\r\nn_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd\r\n-69d4-94d27b597f2f"
    },
    {
      "database_name": "tpchdslitev5",
      "query_count": "56",
      "query_text": "select count(*) as current_Queries\r\n_Running from stv_recents where status = 'Runn",
      "min_execution_time": "0.00",
      "max_execution_time": "1.00",
      "average_executi\r\non_time": "0.01",
      "total_execution_time": "1.00",
      "max_query_id": 1956,
      "last_run_date": "2016-09-01T16:00:00.000Z",
      "aborted": 0,
      "event": null,
      "timestamp": "2016-09-02 06:51:49",
      "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f"
    }
  ]
]

在上面的示例中,我处理了3个结果集。前两个结果集包含一行作为有效负载,最后一行包含11.

我需要将每一行解构为一个(或多个)键/值对,并用它关联的时间戳& GUID。对于前两个结果集,事情是直截了当的,因为我有一个记录,只有一个键/值对可以使用。

我需要处理未知形状的结果集这一事实使事情变得更加困难。在第3个结果集中,我需要将11行中的每一行分成11个键/值对(11行,每行11个字段,不包括guid和时间戳)

这是我最终需要达到的...... enter image description here

这似乎是for和/或for..in的一个非常直接的练习,但是我在拉动属性键和属性值时遇到了问题。

myObject.length

返回3.当然。

myObject[0].length

价值= 1 ......对。

myObject[2][3].query_count

价值= 9 ......还是不错的。但我提前不知道关键。在我通过我的X(在这种情况下是三个)结果集对象和每个包含的yz(一到11个)行中迭代后,如何实际检索表示我的"列的键和值名称"和#34;列值"?

我一直盯着这个太久了,我的大脑不再合作了。我知道当有人把我推向正确的方向时,答案将会是明显的,但是我需要推动。

提前致谢!

2 个答案:

答案 0 :(得分:0)

您的问题有点不清楚,但如果您尝试获取给定对象的键/值对,请尝试以下操作:

var obj = ...;
for (var key in obj) {
  if (obj.hasOwnProperty(key)) { // I don't think this is necessary for parse'd JSON
    var value = obj[key];
    // do something with key and value...
    // maybe add to your table under the key's column? unclear
  }
}

答案 1 :(得分:0)

也许您正在寻找所有分开的项目。

var data = [[{ "current_queries_running": "0", "timestamp": "2016-09-02 06:51:49", "guid": "8d9470cc-669b-97a2-8fd6-7bab0d0c052e" }], [{ "lasthour_queries_spilled_to_disk_count": "0", "timestamp": "2016-09-02 06:51:49", "guid": "d15bd886-e72c-9879-d997-f2d48af4afa6" }], [{ "database_name": "tpchdslitev5", "query_count": "56", "query_text": "SELECT count(distinct query) LastHour_queries_spilled_to_disk_count FROM svl_que", "min_execution_time": "0.00", "max_execution_time": "16.00", "average_execution_time": "0.55", "total_execution_time": "31.00", "max_query_id": 1958, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "51", "query_text": "select trim(database) as database_name, count(query) as query_count, max(substri", "min_execution_time": "0.00", "max_execution_time": "10.00", "average_execution_time": "0.29", "total_execution_time": "15.00", "max_query_id": 1955, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select count(*) ascurrent_transaction_count from svv_transactions t WHERE t.loc", "min_execution_time": "0.00", "max_execution_time": "5.00", "average_execution_time": "1.00", "total_execution_time": "9.00", "max_query_id": 1598, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select case when count(*) > 0 then 1 else 0 end as current_is_vacuum_in_process ", "min_execution_time": "0.00", "max_execution_time": "6.00", "average_execution_time": "0.88", "total_execution_time": "8.00", "max_query_id": 1600, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select count(distinct l.query) as LastHour_perf_alert_count from stl_alert_event", "min_execution_time": "0.00", "max_execution_time": "4.00", "average_execution_time": "0.77", "total_execution_time": "7.00", "max_query_id": 1602, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select sum(total) as LastHour_internal_broadcasting from (select count(query) to", "min_execution_time": "0.00", "max_execution_time": "4.00", "average_execution_time": "0.66", "total_execution_time": "6.00", "max_query_id": 1601, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select count(*) as current_ungranted_locks_on_resources from svv_transactions t ", "min_execution_time": "0.00", "max_execution_time": "4.00", "average_execution_time": "0.55", "total_execution_time": "5.00", "max_query_id": 1599, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select avg(datediff(ms, starttime, endtime)) as lastHour_QueryExecutionTime_sec ", "min_execution_time": "0.00", "max_execution_time": "5.00", "average_execution_time": "0.55", "total_execution_time": "5.00", "max_query_id": 1595, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "SELECT SUM(w.total_queue_time) / 1000000.0 as LastHour_WLM_QueueTime FROM stl_wl", "min_execution_time": "0.00", "max_execution_time": "3.00", "average_execution_time": "0.44", "total_execution_time": "4.00", "max_query_id": 1597, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "9", "query_text": "select avg(datediff(ms, starttime, endtime)) as lastHour_UserQueryExecutionTime_", "min_execution_time": "0.00", "max_execution_time": "2.00", "average_execution_time": "0.22", "total_execution_time": "2.00", "max_query_id": 1596, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }, { "database_name": "tpchdslitev5", "query_count": "56", "query_text": "select count(*) as current_Queries_Running from stv_recents where status = 'Runn", "min_execution_time": "0.00", "max_execution_time": "1.00", "average_execution_time": "0.01", "total_execution_time": "1.00", "max_query_id": 1956, "last_run_date": "2016-09-01T16:00:00.000Z", "aborted": 0, "event": null, "timestamp": "2016-09-02 06:51:49", "guid": "038e8b57-aa5c-81dd-69d4-94d27b597f2f" }]],
    result = [];

data.forEach(function (a) {
    a.forEach(function (b) {
        Object.keys(b).forEach(function (k) {
            var o = { guid: b.guid, timestamp: b.timestamp };
            if (k !== 'guid' && k !== 'timestamp') {
                o.key = k;
                o.value = b[k];
                result.push(o);
            }
        });
    });
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }