lodash json 组对象成本

lodash json group object costs

请 我有这个 JSON 对象,想按类型对值进行分组。

var costs = [
  { 'name': 'JON', 'flight':100, 'value': 12,  type: 'uns' },
  { 'name': 'JON', 'flight':100, 'value': 35,  type: 'sch' },
  { 'name': 'BILL', 'flight':200, 'value': 33,  type: 'uns' },
  { 'name': 'BILL', 'flight':200, 'value': 45,  type: 'sch' }
]; 

我想要这样的东西:

var costs = [
  { 'name': 'JON', 'flight':100, 'uns': 12,  'sch': 35 },
  { 'name': 'BILL', 'flight':200, 'uns': 33,  'sch': 45}
];

我尝试使用 lodash 但没有成功:

var compiled_costs = _.chain(costs)
                      .groupBy("flight")
                      .value();

{
"100":
    [    {"name":"JON","flight":100,"value":12,"type":"uns"},
         {"name":"JON","flight":100,"value":35,"type":"sch"}
    ],
"200":
    [
         {"name":"BILL","flight":200,"value":33,"type":"uns"},  
         {"name":"BILL","flight":200,"value":45,"type":"sch"}
    ]
}

您可以使用本机 reduce() 方法:

const costs = [
  { 'name': 'JON', 'flight':100, 'value': 12,  type: 'uns' },
  { 'name': 'JON', 'flight':100, 'value': 35,  type: 'sch' },
  { 'name': 'BILL', 'flight':200, 'value': 33,  type: 'uns' },
  { 'name': 'BILL', 'flight':200, 'value': 45,  type: 'sch' }
];
const compiledCosts = costs.reduce((acc, { name, flight, value, type }) => {
  let obj = acc.find(x => x.name === name);
  if (typeof obj === 'undefined') {
    acc.push({ name, flight, [type]: value });
  } else {
    obj[type] = value;
  }
  return acc;
}, []);
console.log(compiledCosts);

对于相同命名的对象,您可以在散列 table 上使用闭包。

var data = [{ name: 'JON', flight: 100, value: 12, type: 'uns' }, { name: 'JON', flight: 100, value: 35, type: 'sch' }, { name: 'BILL', flight: 200, value: 33, type: 'uns' }, { name: 'BILL', flight: 200, value: 45, type: 'sch' }],
    grouped = data.reduce(function (hash) {
        return function (r, o) {
            if (!hash[o.name]) {
                hash[o.name] = { name: o.name, flight: o.flight };
                r.push(hash[o.name]);
            }
            hash[o.name][o.type] = (hash[o.name][o.type] || 0) + o.value;
            return r;
        }
    }(Object.create(null)), []);

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

这就是制作地图的目的。由于您正在合并数据,因此您可能需要考虑保留 hashMap 以便更快地查找。否则,正如我所展示的那样,将它变成一个数组是微不足道的。

function compiled_costs(costs) {
  var hash = new Map();
  for (let cost of costs) {
    if (hash.has(cost.name)) {
      var values = hash.get(cost.name),
        value = values.value,
        type = values.type;
      delete values.value;
      delete values.type;
      values.uns = value;
      values.sch = cost.value;
    } else hash.set(cost.name, cost)
  }
  return hash
}

var costs = [{
    'name': 'JON',
    'flight': 100,
    'value': 12,
    type: 'uns'
  },
  {
    'name': 'JON',
    'flight': 100,
    'value': 35,
    type: 'sch'
  },
  {
    'name': 'BILL',
    'flight': 200,
    'value': 33,
    type: 'uns'
  },
  {
    'name': 'BILL',
    'flight': 200,
    'value': 45,
    type: 'sch'
  }
];

var formated = [...compiled_costs(costs).values()];
//formated
console.log('formated',formated);
//hashed
var hashed = [...compiled_costs(costs)];
console.log('hashed',hashed);

var res = _.chain(costs)
    .groupBy('flight') // group costs by flight
    .mapValues(function(flightItems, flight) { // iterate flight arrays
        return { // return obj on each flight array
            name: _.get(flightItems, [0, 'name']), // just get name from first item of flight array
            flight: flight,
            uns: _.chain(flightItems) // get all flight items with type uns and sum items values
                .filter({type: 'uns'})
                .sumBy('value')
                .value(),
            sch: _.chain(flightItems)
                .filter({type: 'sch'})
                .sumBy('value')
                .value()
        }
    })
    .values() // get values from object
    .value();