admin管理员组

文章数量:1430077

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 plicated. If any one can help me, it would be really thankful.

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 plicated. If any one can help me, it would be really thankful.

Share Improve this question asked Aug 20, 2018 at 9:08 RajeshwarRajeshwar 2,5274 gold badges34 silver badges49 bronze badges 1
  • 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 munity too) – Koushik Chatterjee Commented Sep 15, 2018 at 16:07
Add a ment  | 

5 Answers 5

Reset to default 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./ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

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

You can use Object.keys in bination 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);

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

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./ajax/libs/lodash.js/4.17.10/lodash.core.js"></script>

本文标签: javascriptTransform objects to array using lodashStack Overflow