繁体   English   中英

使用lodash将对象转换为数组

[英]Transform objects to array using lodash

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

{
  "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循环,但它变得越来越复杂。 如果有人可以帮助我,那将非常感激。

这是一种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> 

尝试将entries reduce到一个对象数组中,迭代内部对象的每个值:

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

您可以将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); 

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

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

使用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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM