簡體   English   中英

使用lodash轉換嵌套對象

[英]Transform a nested object using lodash

輸入:

const a =  {
            "8": [{
                "strategy": 123,
                "id": 1,
                "config": {
                    "global_dag_conf": {
                        "algo_v2_conf": {
                            "features_to_combine": [],
                            "segments": [],
                            "force_performance": false,
                            "min_bid": 0,
                            "max_bid": 13
                        }
                    }
                }
            }],
            "13": [{
                "strategy": 456,
                "id": 2,
                "config": {
                    "global_dag_conf": {
                        "algo_v2_conf": {
                            "ivr_measured": []
                        }
                    }
                }
            }]

        }

輸出:

{
   "8": [
      {
         "global_dag_conf": {
            "algo_v2_conf": {
               "features_to_combine": [],
               "segments": [],
               "force_performance": false,
               "min_bid": 0,
               "max_bid": 13
            }
         },
         "algo_id": 1
      }
   ],
   "13": [
      {
         "global_dag_conf": {
            "algo_v2_conf": {
               "ivr_measured": []
            }
         },
         "algo_id": 2
      }
   ]
}

我嘗試下面的解決方案工作正常但需要知道是否有更好的方法來使用lodash和JS做到這一點。

result = _.map(_.keys(addtionalAlgos), (algoType) => {
    addtionalAlgos[algoType] = _.map(addtionalAlgos[algoType], v => _.assign(v.config, { algo_id: v.id }));
    return addtionalAlgos;
  })[0]

使用_.mapValues()迭代鍵,使用對象解構擴展語法的 Array.map()重新格式化對象:

 const data = {"8":[{"strategy":123,"id":1,"config":{"global_dag_conf":{"algo_v2_conf":{"features_to_combine":[],"segments":[],"force_performance":false,"min_bid":0,"max_bid":13}}}}],"13":[{"strategy":456,"id":2,"config":{"global_dag_conf":{"algo_v2_conf":{"ivr_measured":[]}}}}]} const result = _.mapValues(data, arr => arr.map(({ id: algo_id, config }) => ({ algo_id, ...config }) )) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script> 

這是一個不使用lodash的解決方案:

  • 使用Object.entries()獲取鍵值對數組
  • 使用reduce over the array創建一個新對象
  • 使用map創建新的對象數組。
  • 構造每個對象以獲取idconfig 傳播 config變量以刪除一級嵌套

 const input = {"8":[{"strategy":123,"id":1,"config":{"global_dag_conf":{"algo_v2_conf":{"features_to_combine":[],"segments":[],"force_performance":false,"min_bid":0,"max_bid":13}}}}],"13":[{"strategy":456,"id":2,"config":{"global_dag_conf":{"algo_v2_conf":{"ivr_measured":[]}}}}]} const output = Object.entries(input) .reduce((r, [key, value]) => { r[key] = value.map(({ id, config }) => ({ algo_id: id, ...config })); return r; }, {}) console.log(output) 

使用mapValuesmapassign方法的純Lodash解決方案

 let data = {"8":[{"strategy":123,"id":1,"config":{"global_dag_conf":{"algo_v2_conf":{"features_to_combine":[],"segments":[],"force_performance":false,"min_bid":0,"max_bid":13}}}}],"13":[{"strategy":456,"id":2,"config":{"global_dag_conf":{"algo_v2_conf":{"ivr_measured":[]}}}}]}; let res = _.mapValues(data, arr => _.map(arr, obj => _.assign({ 'algo_id': obj.id, 'global_dag_conf': obj.config.global_dag_conf }))); console.log(res); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script> 

沒有lodash的替代方案。

函數reduce允許生成將使用函數map填充的對象,該函數map將原始對象轉換為期望的結構。

 const a = { "8": [{ "strategy": 123, "id": 1, "config": { "global_dag_conf": { "algo_v2_conf": { "features_to_combine": [], "segments": [], "force_performance": false, "min_bid": 0, "max_bid": 13 } } } }], "13": [{ "strategy": 456, "id": 2, "config": { "global_dag_conf": { "algo_v2_conf": { "ivr_measured": [] } } } }] }; let result = Object.entries(a).reduce((a, [key, arr]) => { return Object.assign(a, {[key]: arr.map(({id: algo_id, config}) => ({algo_id, ...config}))}); }, Object.create(null)); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM