【问题标题】:Transform objects to array using lodash使用 lodash 将对象转换为数组
【发布时间】:2019-01-26 09:19:12
【问题描述】:

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

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

【问题讨论】:

  • 没有一个答案能满足您的需求?或者正是您正在寻找的!我们可能仍然可以提供帮助! (这也将有助于社区)

标签: javascript arrays object underscore.js lodash


【解决方案1】:

这是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】:

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

    【讨论】:

      【解决方案3】:

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

      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】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 也是一样的:p +1
          猜你喜欢
          • 1970-01-01
          • 2016-08-06
          • 1970-01-01
          • 2019-04-18
          • 2019-08-09
          • 1970-01-01
          • 2015-10-12
          相关资源
          最近更新 更多