遍历一个对象数组并输出自定义对象

时间:2016-09-23 12:02:07

标签: javascript arrays data-structures mapping reduce

我需要从Typescript中的对象数组构建一个自定义对象。

在下面的给出示例中,如何计算salesman_1_name在数组中的次数,为每个Salesman_1_name添加总计,并计算每个salesman_1_name的保修是否为空?如果我有:

    Str.toUpperCase().contains("someexamplestring".toUpperCase());

                              or

    Str.toLowerCase().contains("someexamplestring".toLowerCase());

我需要输出:

[
    {salesman_1_name:Brian, gross:100, warranty: Easycare},
    {salesman_1_name:Brian, gross:100, warranty: Easycare},
    {salesman_1_name:Brian, gross:100, warranty: null},
    {salesman_1_name:Kreso, gross:100, warranty: null},
    {salesman_1_name:Filip, gross:100, warranty: Easycare}
]

4 个答案:

答案 0 :(得分:1)

您可以使用对象作为具有名称的组的引用并进行聚合。

var data = [{ salesman_1_name: 'Brian', gross: 100, warranty: 'Easycare' }, { salesman_1_name: 'Brian', gross: 100, warranty: 'Easycare' }, { salesman_1_name: 'Brian', gross: 100, warranty: null }, { salesman_1_name: 'Kreso', gross: 100, warranty: null }, { salesman_1_name: 'Filip', gross: 100, warranty: 'Easycare' }],
    grouped = [];

data.forEach(function (a) {
    if (!this[a.salesman_1_name]) {
        this[a.salesman_1_name] = { Name: a.salesman_1_name, Count: 0, Gross: 0, WarrantyCount: 0 };
        grouped.push(this[a.salesman_1_name]);
    }
    this[a.salesman_1_name].Count++;
    this[a.salesman_1_name].Gross += a.gross;
    a.warranty !== null && this[a.salesman_1_name].WarrantyCount++;    
}, Object.create(null));

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

答案 1 :(得分:1)

data.reduce((acc, cur) => {
  const obj = acc.find(x => x.Name === cur.salesman_1_name);
  if (obj) {
    obj.Gross += cur.gross;
    obj.WarrantyCount = cur.warranty !== null ? obj.WarrantyCount + 1 : obj.WarrantyCount;
  } else {
    acc.push({ Name: cur.salesman_1_name, Gross: cur.gross, WarrantyCount: cur.warranty !== null ? 1 : 0 });
  }
  return acc;
}, []);

答案 2 :(得分:0)

// Code goes here

var data = [{
  salesman_1_name: 'Brian',
  gross: 100,
  warranty: 'Easycare'
}, {
  salesman_1_name: 'Brian',
  gross: 100,
  warranty: 'Easycare'
}, {
  salesman_1_name: 'Brian',
  gross: 100,
  warranty: null
}, {
  salesman_1_name: 'Kreso',
  gross: 100,
  warranty: null
}, {
  salesman_1_name: 'Filip',
  gross: 100,
  warranty: 'Easycare'
}];


var result = (function(myArray) {
  var obj = []; //also can be used Object.values(dataCollector)
  var dataCollector = myArray.reduce(function(dataCollector, currentItem ) {

    if (!dataCollector[currentItem['salesman_1_name']]) {
      dataCollector[currentItem['salesman_1_name']] = {
        Name: currentItem.salesman_1_name,
        Count: 0,
        Gross: currentItem.gross,
        WarrantyCount: currentItem['salesman_1_name'].warranty != null ? 0 : 1
      };

    } else {
      dataCollector[currentItem['salesman_1_name']].Count++;
      dataCollector[currentItem['salesman_1_name']].Gross += currentItem.gross;
      if (currentItem['salesman_1_name'].warranty !== null) {
        dataCollector[currentItem['salesman_1_name']].WarrantyCount++;
      }

    }

    return dataCollector;

  }, {});

  for (var key in dataCollector) {
    if (dataCollector.hasOwnProperty(key)) {
      obj.push(dataCollector[key]);
    }
  }
  return obj;

})(data)

console.log(result);

答案 3 :(得分:0)



var rawSalesmanData = [
    { salesman_1_name: "Brian", gross: 100, warranty: "Easycare" },
    { salesman_1_name: "Brian", gross: 100, warranty: "Easycare" },
    { salesman_1_name: "Brian", gross: 100, warranty: null },
    { salesman_1_name: "Kreso", gross: 100, warranty: null },
    { salesman_1_name: "Filip", gross: 100, warranty: "Easycare" }
];

var salesmanSpecificList = rawSalesmanData.reduce(function (collector, rawDataItem/*, idx, rawDataList*/) {

    var map       = collector.map;
    var key       = rawDataItem.salesman_1_name;
    var namedItem = map[key];

    if (!namedItem) {
        namedItem = map[key] = {
            name            : key,
            occurencyCount  : 0,
            grossTotal      : 0,
            warrantyCount   : 0
        };
        collector.list.push(namedItem);
    }
    namedItem.occurencyCount++;
    namedItem.grossTotal = (namedItem.grossTotal + rawDataItem.gross);
    namedItem.warrantyCount = (namedItem.warrantyCount + ((rawDataItem.warranty !== null) ? 1 : 0));

    return collector;

}, {

    map : {},
    list: []

}).list;


console.log("salesmanSpecificList : ", salesmanSpecificList);