使用lodash将对象转换为数组

4

我想将一个对象转换为数组格式。 输入的对象是

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

预期输出为

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

我尝试使用for循环,但变得更加复杂了。如果有人能帮助我,我会非常感激。


如果没有任何答案能够满足您的需求,或者正是您正在寻找的!我们仍然可以提供帮助!(这也将有助于社区) - Koushik Chatterjee
5个回答

4
这是一种使用lodash的方式: _.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>


2
尝试使用“reduce”将“entries”减少为对象数组,迭代每个内部对象的值:

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


1
你可以使用Object.keysreduce结合使用。

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


0

使用Lodash,您只需要使用_.reduce()_.forEach()方法,就像这样:

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;
}, []);

示例:

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>


0
你可以将内部值映射到相应的索引。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接