简体   繁体   English

node.js 中的 JSON 迭代

[英]JSON iteration in node.js

[
  {
    "Hs": 5,
    "Type": "Support",
    "By": "William Shake the Speare",
    "Product": "SA",
    "Customer": "Huxley",
    "State": "In progress"
  },
  {
    "Hs": 2,
    "Type": "Support",
    "By": "Orwells",
    "Product": "Web",
    "Customer": "Infranet",
    "State": "Closed"
  }]

I have many of this objects and need to take Customer and sum (add) all of his "Hs" value.我有很多这样的对象,需要获取 Customer 并总结(添加)他的所有“Hs”值。 So the final array would be like:所以最终的数组会是这样的:

[
{
    "Customer" : "Infranet",
    "Total_hs" : "18" 
},
{
    "Customer" : "Huxley",
    "Total_hs" : "11" 
}
]

I tried to find out with lodash and node foreach functions but can't, could you help me please?我试图找出 lodash 和 node foreach 函数,但不能,你能帮我吗? Thanks!谢谢!

First, you must make sure you have the input data as a javascript object (so if it is a json string, it will need to be parsed with something like var items = JSON.parse(input_string) )首先,您必须确保将输入数据作为 javascript 对象(因此,如果它是 json 字符串,则需要使用类似var items = JSON.parse(input_string)进行解析)

var items = [{
  "Hs": 5,
  "Type": "Support",
  "By": "William Shake the Speare",
  "Product": "SA",
  "Customer": "Huxley",
  "State": "In progress"
}, {
  "Hs": 2,
  "Type": "Support",
  "By": "Orwells",
  "Product": "Web",
  "Customer": "Infranet",
  "State": "Closed"
}]

... next, create array of summed Hs values... ...接下来,创建求和的Hs值数组...

var totals = _.reduce(items, function(memo, item) {
  // make sure that the customer is created on the output object, or initialise it to zero
  memo[item.Customer] = memo[item.Customer] || 0;
  // increment the Hs value with the current item's Hs value
  memo[item.Customer] += item.Hs;
  // return the current object for the next iteration of the loop
  return memo;
// pass empty object to initialise reduce
}, {});

totals should now have array of objects, with customer name as key, and total Hs as value. totals 现在应该有对象数组,以客户名称为键,以总 Hs 作为值。

... next, reformat array to match desired data format... ...接下来,重新格式化数组以匹配所需的数据格式...

var target_array = _.map(totals, function(item, key) {
  return {
    Customer: key,
    Total_hs: item
  }
});

... check the out put is correct... ...检查输出是否正确...

console.log(target_array);

Given the JSON is provided as a JavaScript object ( items in the sample below):鉴于 JSON 是作为 JavaScript 对象提供的(以下示例中的items ):

var items = [{
    "Hs": 5,
    "Type": "Support",
    "By": "William Shake the Speare",
    "Product": "SA",
    "Customer": "Huxley",
    "State": "In progress"
  }, {
    "Hs": 2,
    "Type": "Support",
    "By": "Orwells",
    "Product": "Web",
    "Customer": "Infranet",
    "State": "Closed"
}];
var collected = _.map(_.keys(_.grouped(items, "Customer")), function(k) {
  return {
    Customer: k,
    Hs: _.reduce(grouped[k], function(accumulator, v) {
        return accumulator + v.Hs;
      }, 0)
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM