如何消除多次迭代

时间:2017-10-10 03:22:42

标签: javascript json typescript lodash

以下代码以多次迭代所需的方式获取下面的结果。我想知道在一次或多次迭代中实现它的方法是什么。提前致谢。

var input = [{
  "ActiveMembers": [{
    "Id": 101,
    "Name": "alpha"
  }, {
    "Id": 102,
    "Name": "bravo"
  }],
  "Contents": [{
    "Id": 2001,
    "RowId": "517",
    "Time": "19 Jan 2017",
    "ViewCount": 1124
  }, {
    "Id": 2002,
    "RowId": "518",
    "Time": "Today, 07:02 PM",
    "ViewCount": 62
  }],
  "TotalUsers": 3,
  "UsersDetails": "2 members, 1 anonymous users"
}, {
  "ActiveMembers": [{
    "Id": 101,
    "Name": "alpha"
  }, {
    "Id": 103,
    "Name": "charlie"
  }, {
    "Id": 104,
    "Name": "delta"
  }, {
    "Id": 105,
    "Name": "bravo"
  }],
  "Contents": [{
    "Id": 2002,
    "RowId": "519",
    "Time": "27 Jun 2017",
    "ViewCount": 4833
  }, {
    "Id": 2041,
    "RowId": "525",
    "Time": "17 Feb 2015",
    "ViewCount": 24491
  }],
  "TotalUsers": 23,
  "UsersDetails": "4 members, 19 anonymous users"
}];

var contents = Array.prototype.concat.apply([], input.map(i => i.Contents));

var activeMembers = _.uniqBy(Array.prototype.concat.apply([], input.map(i => i.ActiveMembers)), (i) => i.Id);
var totalUsers = number = _.sumBy(input, (i) => i.TotalUsers);
var userDetails = string = input.map(i => i.UsersDetails).join(' ; ');

const result = new Object();
result.Contents = contents;
result.ActiveMembers = activeMembers;
result.TotalUsers = totalUsers;
result.UserDetails = userDetails;

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

结果

{
    "ActiveMembers": [
        {
            "Id": 101,
            "Name": "alpha"
        },
        {
            "Id": 102,
            "Name": "bravo"
        },
        {
            "Id": 103,
            "Name": "charlie"
        },
        {
            "Id": 104,
            "Name": "delta"
        },
        {
            "Id": 105,
            "Name": "bravo"
        }            
    ],
    "Contents": [
        {
            "Id": 2001,
            "RowId": "517",
            "Time": "19 Jan 2017",
            "ViewCount": 1124
        },
        {
            "Id": 2002,
            "RowId": "518",
            "Time": "Today, 07:02 PM",
            "ViewCount": 62
        },
        {
            "Id": 2002,
            "RowId": "519",
            "Time": "27 Jun 2017",
            "ViewCount": 4833
        },
        {
            "Id": 2041,
            "RowId": "525",
            "Time": "17 Feb 2015",
            "ViewCount": 24491
        }            
    ],
    "TotalUsers": 26,
    "UsersDetails": "2 members, 1 anonymous users;4 members, 19 anonymous users"
}

1 个答案:

答案 0 :(得分:0)

在一次迭代中聚合数据。

let ActiveMembers = [];
let Contents = [];
let TotalUsers = 0;
let UserDetails = [];

input.forEach((item) => {
    ActiveMembers = ActiveMembers.concat(item.ActiveMembers);
    Contents = Contents.concat(item.Contents);
    TotalUsers += item.TotalUsers;
    UserDetails.push(item.UsersDetails);
});

const result = {
    ActiveMembers: _.uniqBy(ActiveMembers, "Id"),
    Contents: Contents,
    TotalUsers: TotalUsers,
    UserDetails: UserDetails.join(";")
};

console.log(JSON.stringify(result));