elasticsearch,Javascript,Arrays,Json,elasticsearch" /> elasticsearch,Javascript,Arrays,Json,elasticsearch" />

Javascript 不重复地从JSON数组中获取密钥

Javascript 不重复地从JSON数组中获取密钥,javascript,arrays,json,elasticsearch,Javascript,Arrays,Json,elasticsearch,考虑从发送到Elasticsearch集群的请求返回的以下JSON数组: [ { "_type": "Event example", "_source": { "democarrier_s": "vodafone UK", "m-Ecosystem_s": "iOS", "demo-application": "demo", "demo-country-code

考虑从发送到Elasticsearch集群的请求返回的以下JSON数组:

[
    {
        "_type": "Event example",
        "_source": {
            "democarrier_s": "vodafone UK",
            "m-Ecosystem_s": "iOS",
            "demo-application": "demo",
            "demo-country-code": "GB"
        }
    },
    {
        "_type": "Event example",
        "_source": {
            "democarrier_s": "Verizon",
            "m-Ecosystem_s": "iOS",
            "demo-application": "demo1",
            "demo-country-code": "US"
        }
    }
]
我试图找出如何从本文档中获取所有唯一键,而无需重复和硬编码任何值,并将其存储到以下形式的对象中:

columns = ['_type', '_source.democarrier_s', '_source.m-Ecosystem_s', '_source.demo-application', '_source.demo-country-code'];
有人能帮我想一想如何做到这一点吗?我一直试图循环浏览文档并存储密钥,但我不太明白_类型“”可以硬编码到columns对象中,因为它将始终存在


提前感谢您的帮助和时间。

如果只有两个级别,您可以这样做:

var数据=[
{
“_类型”:“事件示例”,
“_来源”:{
“democarrier_s”:“沃达丰英国”,
“移动生态系统”:“iOS”,
“演示应用程序”:“演示”,
“演示国家代码”:“GB”
}
},
{
“_类型”:“事件示例”,
“_来源”:{
“democarrier_s”:“Verizon”,
“移动生态系统”:“iOS”,
“演示应用程序”:“演示1”,
“演示国家代码”:“美国”
}
}
]
var键=[];
data.forEach(功能(项){
用于(var输入项){
var-hasProperties=false;
如果(项目类型[键]!=='string'){
对于(项目[key]中的变量key2){
hasProperties=true;
var keyName=key+““+key2;
if(keys.indexOf(keyName)<0)
按键。按(按键名称);
}
}
如果(!hasProperties&&keys.indexOf(key)<0){
按键。按(键);
}
}
});
forEach(函数(k){console.log(k)})