1

Input:

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": []
                        }
                    }
                }
            }]

        }

Output:

{
   "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
      }
   ]
}

I tried below solution which works fine but need to know if is there any better way to do this using lodash and JS.

result = _.map(_.keys(addtionalAlgos), (algoType) => {
    addtionalAlgos[algoType] = _.map(addtionalAlgos[algoType], v => _.assign(v.config, { algo_id: v.id }));
    return addtionalAlgos;
  })[0]
1
  • What is the relation between input & output? Commented Mar 23, 2019 at 13:56

4 Answers 4

1

Here's a solution without using lodash:

  • Use Object.entries() to get an array of key-value pairs
  • Create a new object by using reduce over the array
  • Use map to create a new array of objects.
  • Destructure each object to get id and config. Spread the config variable to remove one level of nesting

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)

Sign up to request clarification or add additional context in comments.

Comments

1

Use _.mapValues() to iterate the keys, and Array.map() with object destructuring and spread syntax to reformat the object:

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>

Comments

1

A pure Lodash solution using mapValues, map and assign methods

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>

Comments

0

An alternative without lodash.

The function reduce allows to generate an object which will be filled using the function map which transforms the original objects to the desired structure.

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.