【问题标题】:underscore map key name w/ increment带增量的下划线映射键名
【发布时间】:2018-01-25 23:23:05
【问题描述】:

我有一个像这样的 js 对象:

[{"product":7001,"quantity":1},{"product":3002,"quantity":1},{"product":4002,"quantity":4}]

我需要能够逐步重命名键,例如:

[{"product":7001,"quantity":1},{"product1":3002,"quantity1":1},{"product2":4002,"quantity2":4}]

我一直在努力为键名添加增量,但这不适用于我正在使用的对象类型。

任何建议将不胜感激。泰。

/*
this doesn't work
var a = [
  {product : "3002", quantity: 1},
  {product : "4001", quantity : 3}
  ];
*/

// this updates the key, but not in the way I need it to.
var a =  {product : "3002", quantity: 1}

  var n = 0;

  function increment(string)
  {
    n++
      return string + n;
  }

  var b = {};

  var map = {
      name : "name"
  };

  var keymap = {};
  _.each(a, function(value, key) {
      var oldkey = key;
      key = increment(key);
      keymap[oldkey] = key;
  });
  _.each(a, function(value, key) {
      key = keymap[key] || key;
      b[key] = value;
  });

  console.log('increment keys: ' + JSON.stringify(b));

【问题讨论】:

    标签: javascript arrays dictionary underscore.js increment


    【解决方案1】:

    似乎是一个不寻常的请求和不寻常的结构,但这是一个产生预期结果的 map/reduce

    var data = [{"product":7001,"quantity":1},{"product":3002,"quantity":1},{"product":4002,"quantity":4}],
    
    res = data.map((o,i)=>Object.keys(o).reduce((o2, key)=> i ? (o2[key+i] = o[key],o2):o,{}))
    
    console.log(res)

    【讨论】:

    • 这响应较晚,但是否有可能从 1 开始获得增量,例如"product1":"quantity1" ?
    【解决方案2】:

    我有个更好的主意。为什么不向对象添加 ID 键而不是更改键名。以下代码循环遍历数组并创建一个新数组。新数组中的对象现在有一个 id 键,对应于产品的索引。

        let products = [{"product":7001,"quantity":1},{"product":3002,"quantity":1},{"product":4002,"quantity":4}];
    
        products = products.map((item, index) => Object.assign(item, { id: index + 1 }));
    
        console.log(products); 

    【讨论】:

      【解决方案3】:

      使用_.map() 迭代数组。因为 underscore 缺乏映射键的能力,你可以_.invert() 键和值,用_.mapObject() 更新键(当前值),然后再次_.invert()

      var data = [{"product":7001,"quantity":1},{"product":3002,"quantity":1},{"product":4002,"quantity":4}]
      
      var result = _.map(data, function(o, i) {
        return _.chain(o)
          .invert()
          .mapObject(function(v) { return v + (i || '') })
          .invert()
          .value()
      })
      
      console.log(result)
      <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

      在 ES6 中使用 Array.map()computed property names 更容易做到这一点:

      var data = [{"product":7001,"quantity":1},{"product":3002,"quantity":1},{"product":4002,"quantity":4}]
      
      var result = data.map(({ product, quantity }, i) => ({
        [`product${i || ''}`]: product,
        [`quantity${i || ''}`]: quantity
      }));
      
      console.log(result)
      <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

      【讨论】:

        猜你喜欢
        • 2021-12-29
        • 2018-09-18
        • 1970-01-01
        • 2014-06-09
        • 2023-03-19
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 2013-01-05
        相关资源
        最近更新 更多