[英]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
創建新的對象數組。 id
和config
。 傳播 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)
使用mapValues , map和assign方法的純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.