4

I would like to transform an object to an array format. The input object is

{
  "index": {
    "0": 40,
    "1": 242
  },
  "TID": {
    "0": "11",
    "1": "22"
  },
  "DepartureCity": {
    "0": "MCI",
    "1": "CVG"
  },
  "ArrivalCity": {
    "0": "SFB",
    "1": "LAS"
  },
  "Price": {
    "0": 90,
    "1": 98
  }
}

And the expected output is

[
  {
    "index": 40,
    "TID": "11",
    "DepartureCity": "MCI",
    "ArrivalCity": "SFB",
    "Price": 90
  },
  {
    "index": 242,
    "TID": "22",
    "DepartureCity": "CVG",
    "ArrivalCity": "LAS",
    "Price": 98
  }
]

I tried using for loops, but it is getting more complicated. If any one can help me, it would be really thankful.

Rajeshwar
  • 2,290
  • 4
  • 31
  • 41
  • Dose none of the answers fulfills what you need? or exactly what you are looking for! we may able to still help! (and that will help the community too) – Koushik Chatterjee Sep 15 '18 at 16:07

5 Answers5

4

Here is a lodash approach

_.merge([], ..._.map(obj, (v, k) => _.mapValues(v, ev=> ({[k]:ev}))))

let inputObj = {
  "index": {
    "0": 40,
    "1": 242
  },
  "TID": {
    "0": "11",
    "1": "22"
  },
  "DepartureCity": {
    "0": "MCI",
    "1": "CVG"
  },
  "ArrivalCity": {
    "0": "SFB",
    "1": "LAS"
  },
  "Price": {
    "0": 90,
    "1": 98
  }
};

let res = _.merge([], ..._.map(inputObj, (v, k) => _.mapValues(v, ev=> ({[k] :ev}))));

console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32
2

Try reduce-ing the entries into an array of objects, iterating over each value of the inner objects:

const input={"index":{"0":40,"1":242},"TID":{"0":"11","1":"22"},"DepartureCity":{"0":"MCI","1":"CVG"},"ArrivalCity":{"0":"SFB","1":"LAS"},"Price":{"0":90,"1":98}}

const output = Object.entries(input).reduce((a, [key, obj]) => {
  Object.values(obj).forEach((val, i) => {
    if (!a[i]) a[i] = {};
    a[i][key] = val;
  });
  return a;
}, []);
console.log(output);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

You can use Object.keys in combination with reduce

let object = { "index": { "0": 40, "1": 242 }, "TID": { "0": "11", "1": "22" }, "DepartureCity": { "0": "MCI", "1": "CVG" }, "ArrivalCity": { "0": "SFB", "1": "LAS" }, "Price": { "0": 90, "1": 98 } }

result = Object.keys(object['index']).map(function(key){
  return Object.keys(object).reduce(function(obj, item){
    obj[item] = object[item][key];
    return obj;
  }, {});
});
console.log(result);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

You could map the inner values to the according index.

var data = { index: { 0: 40, 1: 242 }, TID: { 0: "11", 1: "22" }, DepartureCity: { 0: "MCI", 1: "CVG" }, ArrivalCity: { 0: "SFB", 1: "LAS" }, Price: { 0: 90, 1: 98 } },
    result = Object
        .entries(data)
        .reduce(
            (r, [k, o]) => Object
                .entries(o)
                .map(([i, v]) => Object.assign(r[i] || {}, { [k]: v })),
            []
        );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

With lodash you just need to use _.reduce() and _.forEach() methods, like this:

const result = _.reduce(Object.entries(object), function(a, [key, obj]){
  _.forEach(Object.values(obj), function(val, i){
    if (!a[i]) a[i] = {};
    a[i][key] = val;
  });
  return a;
}, []);

Demo:

const object = {
  "index": {
    "0": 40,
    "1": 242
  },
  "TID": {
    "0": "11",
    "1": "22"
  },
  "DepartureCity": {
    "0": "MCI",
    "1": "CVG"
  },
  "ArrivalCity": {
    "0": "SFB",
    "1": "LAS"
  },
  "Price": {
    "0": 90,
    "1": 98
  }
};

const result = _.reduce(Object.entries(object), function(a, [key, obj]){
  _.forEach(Object.values(obj), function(val, i){
    if (!a[i]) a[i] = {};
    a[i][key] = val;
  });
  return a;
}, []);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.js"></script>
cнŝdk
  • 31,391
  • 7
  • 56
  • 78