我从包含JSON数组的API得到以下响应,我试图删除除title,column和searchSourceJSON之外的所有元素:
"hits": [
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Integration-Enabled",
"_score": 1,
"_source": {
"title": "demo Media Integration - Enabled",
"description": "",
"hits": 0,
"columns": [
"_source"
],
"sort": [
"timestamp",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"value\":\"Media Integration\",\"disabled\":false},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Integration\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Action\",\"value\":\"Enable\",\"disabled\":false},\"query\":{\"match\":{\"Action\":{\"query\":\"Enable\",\"type\":\"phrase\"}}}}],\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}"
}
}
},
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Import-True",
"_score": 1,
"_source": {
"title": "demo Media Import - True",
"description": "",
"hits": 0,
"columns": [
"FormFactor",
"_type",
"Identity"
],
"sort": [
"_type",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"*\"}},\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"disabled\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"negate\":false,\"value\":\"Media Import\"},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Import\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Successful\",\"value\":\"True\",\"disabled\":false},\"query\":{\"match\":{\"Successful\":{\"query\":\"True\",\"type\":\"phrase\"}}}}]}"
}
}
}
]
有人可以帮我弄清楚如何使用JavaScript从这个JSON文档中删除不需要的元素吗?
编辑:回答,谢谢大家的好消息!
答案 0 :(得分:3)
一种省力的方法是使用Array#Map
,然后只选择你想要的元素。
var hits = [
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Integration-Enabled",
"_score": 1,
"_source": {
"title": "demo Media Integration - Enabled",
"description": "",
"hits": 0,
"columns": [
"_source"
],
"sort": [
"timestamp",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"value\":\"Media Integration\",\"disabled\":false},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Integration\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Action\",\"value\":\"Enable\",\"disabled\":false},\"query\":{\"match\":{\"Action\":{\"query\":\"Enable\",\"type\":\"phrase\"}}}}],\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}"
}
}
},
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Import-True",
"_score": 1,
"_source": {
"title": "demo Media Import - True",
"description": "",
"hits": 0,
"columns": [
"FormFactor",
"_type",
"Identity"
],
"sort": [
"_type",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"*\"}},\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"disabled\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"negate\":false,\"value\":\"Media Import\"},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Import\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Successful\",\"value\":\"True\",\"disabled\":false},\"query\":{\"match\":{\"Successful\":{\"query\":\"True\",\"type\":\"phrase\"}}}}]}"
}
}
}
]
var filtered = hits.map(function(hit){
return { title : hit._source.title, searchSourceJSON : hit._source.exampleSavedObjectMeta.searchSourceJSON, columns : hit._source.columns}
})
console.log(filtered)

答案 1 :(得分:1)
你可以:
使用map方法横切所有结果(这往往代价很高)。
或者因为它似乎是要求Elastic Search过滤掉ES发送的响应。因此,网络流量减少,您可以避免清理数据。
您可以这样添加filter path on your query string:
x[,c("Alley", "Fence")] <- lapply(x[,c("Alley", "Fence")], function(x){`levels<-`(addNA(x), c(levels(x), "None"))})
答案 2 :(得分:0)
您可以解析JSON,然后映射以创建一个包含仅具有所需属性的对象的新数组:
const data = JSON.parse(yourJSON);
const hits = data.hits.map(hit => ({
title: hit._source.title,
searchSourceJSON: hit._source.exampleSavedObjectMeta.searchSourceJSON
});
答案 3 :(得分:0)
var result=(function(){//IIFE to enshure garbage collection
var hits=[...];//your data
return hits.map(hit=>{hit["_source"].title,hit["_source"].colums,hit.exampleSavedObjectMeta.searchSourceJSON});
});
这将删除所需的所有属性(垃圾收集它们)。
答案 4 :(得分:0)
试试这个:
var jsonObj = [
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Integration-Enabled",
"_score": 1,
"_source": {
"title": "demo Media Integration - Enabled",
"description": "",
"hits": 0,
"columns": [
"_source"
],
"sort": [
"timestamp",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"value\":\"Media Integration\",\"disabled\":false},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Integration\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Action\",\"value\":\"Enable\",\"disabled\":false},\"query\":{\"match\":{\"Action\":{\"query\":\"Enable\",\"type\":\"phrase\"}}}}],\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}"
}
}
},
{
"_index": ".example_demo",
"_type": "search",
"_id": "demo-Media-Import-True",
"_score": 1,
"_source": {
"title": "demo Media Import - True",
"description": "",
"hits": 0,
"columns": [
"FormFactor",
"_type",
"Identity"
],
"sort": [
"_type",
"asc"
],
"version": 1,
"exampleSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"[demo-]YYYY.MM\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"*\"}},\"highlight\":{\"pre_tags\":[\"@example-highlighted-field@\"],\"post_tags\":[\"@/example-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647},\"filter\":[{\"meta\":{\"disabled\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"_type\",\"negate\":false,\"value\":\"Media Import\"},\"query\":{\"match\":{\"_type\":{\"query\":\"Media Import\",\"type\":\"phrase\"}}}},{\"meta\":{\"negate\":false,\"index\":\"[demo-]YYYY.MM\",\"key\":\"Successful\",\"value\":\"True\",\"disabled\":false},\"query\":{\"match\":{\"Successful\":{\"query\":\"True\",\"type\":\"phrase\"}}}}]}"
}
}
}
];
var res = jsonObj.map(function(item) {
return {
"title": item._source.title,
"columns": item._source.columns,
"searchSourceJSON": item._source.exampleSavedObjectMeta.searchSourceJSON
};
});
console.log(res);
&#13;