Javascript marge和slice json

时间:2017-08-04 00:38:02

标签: javascript arrays json

基于此数组json我需要不断合并它。 使用push()我得到了这种结果

JSON
**0:**{
    "net_drops.vpn0": {
        "name":"net_drops.vpn0",
        "context":"net.drops",
        "units":"drops/s",
        "last_updated": 1501806176,
        "dimensions": {
            "inbound": {
                "name": "inbound",
                "value": 0.0000000
            },
            "outbound": {
                "name": "outbound",
                "value": 0.0000000
            }
        }
    },
    "named_local.view_resolver_numfetch__default": {
        "name":"named_local.view_resolver_numfetch__default",
        "context":"named.resolver_active_queries",
        "units":"queries",
        "last_updated": 1501806176,
        "dimensions": {
            "queries": {
                "name": "queries",
                "value": 0.0000000
            }
        }
    }

}
**1:**{
    "net_drops.vpn0": {
        "name":"net_drops.vpn0",
        "context":"net.drops",
        "units":"drops/s",
        "last_updated": 1501806176,
        "dimensions": {
            "inbound": {
                "name": "inbound",
                "value": 0.0000000
            },
            "outbound": {
                "name": "outbound",
                "value": 0.0000000
            }
        }
    },
    "named_local.view_resolver_numfetch__default": {
        "name":"named_local.view_resolver_numfetch__default",
        "context":"named.resolver_active_queries",
        "units":"queries",
        "last_updated": 1501806176,
        "dimensions": {
            "queries": {
                "name": "queries",
                "value": 0.0000000
            }
        }
    }

}
...

但我需要这种结果

JSON
{
    "net_drops.vpn0": {
        **0:**{"name":"net_drops.vpn0",
        "context":"net.drops",
        "units":"drops/s",
        "last_updated": 1501806176,
        "dimensions": {
            "inbound": {
                "name": "inbound",
                "value": 0.0000000
            },
            "outbound": {
                "name": "outbound",
                "value": 0.0000000
            }
        }},
        **1:**{"name":"net_drops.vpn0",
        "context":"net.drops",
        "units":"drops/s",
        "last_updated": 1501806176,
        "dimensions": {
            "inbound": {
                "name": "inbound",
                "value": 0.0000000
            },
            "outbound": {
                "name": "outbound",
                "value": 0.0000000
            }
        }}      
    },
    "named_local.view_resolver_numfetch__default": {
        **0:**{"name":"named_local.view_resolver_numfetch__default",
        "context":"named.resolver_active_queries",
        "units":"queries",
        "last_updated": 1501806176,
        "dimensions": {
            "queries": {
                "name": "queries",
                "value": 0.0000000
            }
        }},
        **1:**{"name":"named_local.view_resolver_numfetch__default",
        "context":"named.resolver_active_queries",
        "units":"queries",
        "last_updated": 1501806176,
        "dimensions": {
            "queries": {
                "name": "queries",
                "value": 0.0000000
            }
        }}      
    }
    ...
}

有可能吗?如果是,我该怎么办?

然后我需要根据last_update切片对象,如果last_update超过当前时间的30分钟被切片 例如:当前时间10:00:00,对象last_update 09:31:00被切片。 可能吗?如果是,我该怎么办? 最好的问候

1 个答案:

答案 0 :(得分:0)

看起来您正在使用相同的密钥组合数据。

要实现该结果,请循环访问原始JSON数据并按键将其推送到另一个数组。在您的情况下,密钥是:' net_drops.vpn0'和' named_local.view_resolver_numfetch__default'喜欢:array.net_drops.vpn0 = json_data

对于基于' last_updated'的切片键,您可以尝试使用array.filter功能。请查看此LINK以供参考。