使用lodash将对象转换为数组

时间:2018-08-20 09:08:31

标签: javascript arrays object underscore.js 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循环,但是它变得越来越复杂。如果有人可以帮助我,那将非常感激。

5 个答案:

答案 0 :(得分:4)

这是一种const postItems = this.props.posts.map(post => ( <div key={post.id} className="row"> <div className="container"> <h3>{post.title}</h3> <p>{post.body}</p> <button onClick={() =>this.editPost(post)} className="btn btn-primary"> Edit </button>&nbsp; <button onClick={() =>this.deletePost(post.id)} className="btn btn-danger"> Delete </button> </div> </div> )) 方法

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

答案 1 :(得分: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);

答案 2 :(得分: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);

答案 3 :(得分: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; }

答案 4 :(得分: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>