0

I have users array with their name,

var users = [{'name':'zulekha'}, {'name':'deepika'}];

I am fetching worklogged by each user from jira APIs. So I am getting object like this.

var worklogResult = {
  "issues": [
    {
      "fields": {
        "worklog": {
          "worklogs": [
            {
              "author": {
                "name": "zulekha",
              },
              "timeSpentSeconds": 180
            },
            {
              "author": {
                "name": "deepika",
              },
              "timeSpentSeconds": 210
            }
          ]
        }
      }
    },
    {
      "fields": {
        "worklog": {
          "worklogs": [
            {
              "author": {
                "name": "deepika",
              },
              "timeSpentSeconds": 140
            }
          ]
        }
      }
    },
    {
      "fields": {
        "worklog": {
          "worklogs": [
            {
              "author": {
                "name": "zulekha",
              },
              "timeSpentSeconds": 600,
            }
          ]
        }
      }
    },
    {
      "fields": {
        "worklog": {
          "worklogs": []
        }
      }
    }
  ]
}

Now I want to match worklogResult with users in such a way that I can get following output.

output = [{'name':'zulekha','timeSpentSeconds':780}, {'name':'deepika', 'timeSpentSeconds':350}]

Can anyone suggest me how to achieve this?

1 Answer 1

1

use _.flatMap to flat nested objects

_.chain(worklogResult.issues)
    .flatMap('fields.worklog.worklogs')
    .thru(function(spents) {
        return _.map(users, function(user) {
            return _.merge(user, {
                timeSpentSeconds: _.chain(spents)
                    .filter(['author.name', user.name])
                    .map('timeSpentSeconds')
                    .sum()
                    .value()
            })
        })
    })
    .value()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.