Lodash - 将实体/键/值数组映射到具有属性的Object

时间:2016-10-20 03:55:20

标签: arrays lodash

我有以下数组:

[
  {
    "name": "ABC",
    "type": "iPhone",
    "sessions": "3.0"
  },
  {
    "name": "ABC",
    "type": "Android",
    "sessions": "25.0"
  },
  {
    "name": "ABC",
    "type": "Windows",
    "sessions": "3.0"
  },
  {
    "name": "XYZ",
    "type": "iPhone",
    "sessions": "1.0"
  },
  {
    "name": "XYZ",
    "type": "Android",
    "sessions": "11.0"
  },
  {
    "name": "XYZ",
    "type": "Windows",
    "sessions": "15.0"
  },
  {
    "name": "XYZ",
    "type": "Outlook",
    "sessions": "10.0"
  },
  {
    "name": "DEF",
    "type": "Windows",
    "sessions": "10.0"
  },
  {
    "name": "GHI",
    "type": "iPhone",
    "sessions": "10.0"
  }
]

我想使用lodash:

将其转换为如下对象数组
[
  {
    "name": "ABC",
    "iPhone": "3.0",
    "Android": "25.0",
    "Windows": "3.0",
    "Outlook": ""
  },
  {
    "name": "XYZ",
    "iPhone": "1.0",
    "Android": "11.0",
    "Windows": "15.0",
    "Outlook": "10.0"
  },
  {
    "name": "DEF",
    "iPhone": "",
    "Android": "",
    "Windows": "10.0",
    "Outlook": ""
  },
  {
    "name": "GHI",
    "iPhone": "10.0",
    "Android": "",
    "Windows": "",
    "Outlook": ""
  }
]

基本上,“name”属性必须用作将属性合并到对象中的键。用lodash做任何简洁的方法吗?

3 个答案:

答案 0 :(得分:1)

您可以使用lodash的_.transform()

var arr = [{"name":"ABC","type":"iPhone","sessions":"3.0"},{"name":"ABC","type":"Android","sessions":"25.0"},{"name":"ABC","type":"Windows","sessions":"3.0"},{"name":"XYZ","type":"iPhone","sessions":"1.0"},{"name":"XYZ","type":"Android","sessions":"11.0"},{"name":"XYZ","type":"Windows","sessions":"15.0"},{"name":"XYZ","type":"Outlook","sessions":"10.0"},{"name":"DEF","type":"Windows","sessions":"10.0"},{"name":"GHI","type":"iPhone","sessions":"10.0"}];

var result = _.transform(arr, function(products, item) {
    var product = products[item.name] = products[item.name] || {
      name: item.name,
      iPhone: "",
      Android: "",
      Windows: "",
      Outlook: ""
    };

    product[item.type] = item.sessions;

    return products;
  }, {});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

答案 1 :(得分:0)

另一种解决方案可能是:

var f = [{
  "name": "ABC",
  "type": "iPhone",
  "sessions": "3.0"
}, {
  "name": "ABC",
  "type": "Android",
  "sessions": "25.0"
}, {
  "name": "ABC",
  "type": "Windows",
  "sessions": "3.0"
}, {
  "name": "XYZ",
  "type": "iPhone",
  "sessions": "1.0"
}, {
  "name": "XYZ",
  "type": "Android",
  "sessions": "11.0"
}, {
  "name": "XYZ",
  "type": "Windows",
  "sessions": "15.0"
}, {
  "name": "XYZ",
  "type": "Outlook",
  "sessions": "10.0"
}, {
  "name": "DEF",
  "type": "Windows",
  "sessions": "10.0"
}, {
  "name": "GHI",
  "type": "iPhone",
  "sessions": "10.0"
}];

var def = {
  "name": "",
  "iPhone": "",
  "Android": "",
  "Windows": "",
  "Outlook": ""
};

function updateMemo(memo, obj) {
  memo[obj.name] = memo[obj.name] || _.extend({}, def, {name: obj.name});
  memo[obj.name][obj.type] = obj.sessions;
  return memo;
}

console.log(_.values(_.reduce(f, updateMemo, {})));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

它包括对存储中间对象中的值的数组执行减少。存储的值遵循默认结构。一旦完成数组,只返回值以获得预期的输出。

希望它有所帮助。

答案 2 :(得分:0)

有可能使用lodash mapKeys方法,但是您的值应该是对象而不是原始值。

let orig = {" a ":{rate: 1}, " b ":{rate: 2}};

let result = _.mapKeys(orig, (value, key)=> {
  
  value.rate = value.rate + 1
  return key.trim()
})

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>