javascript эффективное удаление элементов из объекта - PullRequest
0 голосов
/ 04 августа 2020

У меня есть объект javascript, возвращенный из запроса elastcisearch, который выглядит следующим образом:

let resp = {
  'var1': 'ex',
  "aggregations": {
    "terms_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [{
        "key": "118917059",
        "doc_count": 4,
        "top_hits_agg": {
          "hits": {
            "total": {
              "value": 4,
              "relation": "eq"
            },
            "max_score": 14.090103,
            "hits": [{
              "_index": "x-18-07-20201",
              "_type": "_doc",
              "_id": "2",
              "_score": 14.090103,
              "_source": {
                "default_asin": null,
                "scraping_status": null,
                "sku": "118917059"
              }
            }]
          }
        }
      },
      {
        "key": "20584263",
        "doc_count": 4,
        "top_hits_agg": {
          "hits": {
            "total": {
              "value": 4,
              "relation": "eq"
            },
            "max_score": 14.090103,
            "hits": [{
              "_index": "x-18-07-20201",
              "_type": "_doc",
              "_id": "1",
              "_score": 14.090103,
              "_source": {
                "default_asin": null,
                "scraping_status": null,
                "sku": "20584263"
              }
            }]
          }
        }
      },
      {
        "key": "253981722",
        "doc_count": 4,
        "top_hits_agg": {
          "hits": {
            "total": {
              "value": 4,
              "relation": "eq"
            },
            "max_score": 14.090103,
            "hits": [{
              "_index": "x-18-07-20201",
              "_type": "_doc",
              "_id": "3",
              "_score": 14.090103,
              "_source": {
                "default_asin": null,
                "scraping_status": null,
                "sku": "253981722"
              }
            }]
          }
        }
      },
    }
  }
}

Мне нужен только объект hits.hits [0] для каждого элемента в списке aggregations.buckets. Итак, я пытаюсь преобразовать свой объект resp в объект newResp, который будет выглядеть следующим образом:

newResp = [
{
  "_index": "x-18-07-20201",
  "_type": "_doc",
  "_id": "2",
  "_score": 14.090103,
  "_source": {
    "default_asin": null,
    "scraping_status": null,
    "sku": "118917059"
  }
},
{
  "_index": "x-18-07-20201",
  "_type": "_doc",
  "_id": "1",
  "_score": 14.090103,
  "_source": {
    "default_asin": null,
    "scraping_status": null,
    "sku": "20584263"
  }
},
{
  "_index": "x-18-07-20201",
  "_type": "_doc",
  "_id": "3",
  "_score": 14.090103,
  "_source": {
    "default_asin": null,
    "scraping_status": null,
    "sku": "253981722"
  }
},
]
]

Каков наиболее эффективный способ go сделать это? Первое решение, которое приходит мне в голову, - это метод грубой силы вроде этого:

let newResp = []
for (item in resp.aggregations.buckets) {
  newResp.append(item.hits.hits[0)
}

Но есть ли более чистое решение для создания newResp?

Ответы [ 2 ]

0 голосов
/ 04 августа 2020

Просто map() ит.

let resp = {
  'var1': 'ex',
  "aggregations": {
    "terms_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [{
          "key": "118917059",
          "doc_count": 4,
          "top_hits_agg": {
            "hits": {
              "total": {
                "value": 4,
                "relation": "eq"
              },
              "max_score": 14.090103,
              "hits": [{
                "_index": "x-18-07-20201",
                "_type": "_doc",
                "_id": "2",
                "_score": 14.090103,
                "_source": {
                  "default_asin": null,
                  "scraping_status": null,
                  "sku": "118917059"
                }
              }]
            }
          }
        },
        {
          "key": "20584263",
          "doc_count": 4,
          "top_hits_agg": {
            "hits": {
              "total": {
                "value": 4,
                "relation": "eq"
              },
              "max_score": 14.090103,
              "hits": [{
                "_index": "x-18-07-20201",
                "_type": "_doc",
                "_id": "1",
                "_score": 14.090103,
                "_source": {
                  "default_asin": null,
                  "scraping_status": null,
                  "sku": "20584263"
                }
              }]
            }
          }
        },
        {
          "key": "253981722",
          "doc_count": 4,
          "top_hits_agg": {
            "hits": {
              "total": {
                "value": 4,
                "relation": "eq"
              },
              "max_score": 14.090103,
              "hits": [{
                "_index": "x-18-07-20201",
                "_type": "_doc",
                "_id": "3",
                "_score": 14.090103,
                "_source": {
                  "default_asin": null,
                  "scraping_status": null,
                  "sku": "253981722"
                }
              }]
            }
          }
        },
      ]
    }
  }
}

const result = resp
  .aggregations
  .terms_agg
  .buckets
  .map(bucket => bucket.top_hits_agg.hits.hits[0]);

console.log(result);
0 голосов
/ 04 августа 2020

Просто map() ит.

let resp = {
  'var1': 'ex',
  "aggregations": {
    "terms_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [{
        "key": "118917059",
        "doc_count": 4,
        "top_hits_agg": {
          "hits": {
            "total": {
              "value": 4,
              "relation": "eq"
            },
            "max_score": 14.090103,
            "hits": [{
              "_index": "x-18-07-20201",
              "_type": "_doc",
              "_id": "2",
              "_score": 14.090103,
              "_source": {
                "default_asin": null,
                "scraping_status": null,
                "sku": "118917059"
              }
            }]
          }
        }
      },
      {
        "key": "20584263",
        "doc_count": 4,
        "top_hits_agg": {
          "hits": {
            "total": {
              "value": 4,
              "relation": "eq"
            },
            "max_score": 14.090103,
            "hits": [{
              "_index": "x-18-07-20201",
              "_type": "_doc",
              "_id": "1",
              "_score": 14.090103,
              "_source": {
                "default_asin": null,
                "scraping_status": null,
                "sku": "20584263"
              }
            }]
          }
        }
      },
      {
        "key": "253981722",
        "doc_count": 4,
        "top_hits_agg": {
          "hits": {
            "total": {
              "value": 4,
              "relation": "eq"
            },
            "max_score": 14.090103,
            "hits": [{
              "_index": "x-18-07-20201",
              "_type": "_doc",
              "_id": "3",
              "_score": 14.090103,
              "_source": {
                "default_asin": null,
                "scraping_status": null,
                "sku": "253981722"
              }
            }]
          }
        }
      },
      ]
    }
  }
}

const result = resp.aggregations.terms_agg.buckets.map(bucket => bucket.top_hits_agg.hits.hits[0]);

console.log(result);
...