0

So i have an array of object from mongodb query like below,its contained the value from $agg:

[
  {
    "_id": "17000691",
    "latestAnswer": [
      {
        "title": "Test",
        "date": "2022-11-16",
        "position": "Foreman",
        "department": "Production",
        "username": "17000691"
      }
    ]
  },
  {
    "_id": "MT220047",
    "latestAnswer": [
      {
        "title": "Test",
        "date": "2022-11-15",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      },
      {
        "title": "Test",
        "date": "2022-11-16",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      },
      {
        "title": "Test",
        "date": "2022-11-16",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      },
      {
        "title": "Untitled Form1",
        "date": "2022-11-16",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      }
    ]
  }
]

is there a possible way to group the data base on date, title and username into this?

[
  {
    "username": "MT220047",
    "title": "Test",
    "position": "Management Trainee",
    "department": "Logistic",
    "2022-11-15": 1,
    "2022-11-16": 2
  },
  {
    "username": MT220047,
    "title": UntitledForm1,
    "position": "Management Trainee",
    "department": "Logistic",
    "2022-11-16": 1
  },
  {
    "username": 17000691,
    "title": Test,
    "position": "Foreman",
    "department": "Production",
    "2022-11-16": 1,
    
  }
]

any help on this will be useful for mee... thanks.., it try to mapping it but seems little confused bout the logic to group it as i want

2
  • Can you show us the code, which you used to try to map it? Commented Nov 16, 2022 at 8:16
  • okay waitt will provide it on the questrion Commented Nov 16, 2022 at 8:17

1 Answer 1

1

Something like this could work?

const payload = [
  {
    "_id": "17000691",
    "latestAnswer": [
      {
        "title": "Test",
        "date": "2022-11-16",
        "position": "Foreman",
        "department": "Production",
        "username": "17000691"
      }
    ]
  },
  {
    "_id": "MT220047",
    "latestAnswer": [
      {
        "title": "Test",
        "date": "2022-11-15",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      },
      {
        "title": "Test",
        "date": "2022-11-16",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      },
      {
        "title": "Test",
        "date": "2022-11-16",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      },
      {
        "title": "Untitled Form1",
        "date": "2022-11-16",
        "position": "Management Trainee",
        "department": "Logistic",
        "username": "MT220047"
      }
    ]
  }
]

const accumulator = {};
const final = []
const res = payload
.map((payload) => payload.latestAnswer)
.reduce((acc, curr) => { acc.push(...curr); return acc}, []);
res.forEach((item) => {
  const {username, title, position, department, date} = item
  const key = `${username}-${department}-${position}-${title}`
  accumulator[key] =  accumulator[key] || {};
  accumulator[key][date] = accumulator[key][date] + 1 || 1
})

Object.keys(accumulator).forEach((item) => {
  const [username, department, position, title] = item.split('-')
  final.push({
    username, 
    department, 
    position, 
    title,
    ...accumulator[item]
  })
})

For sure can be improved but you should get the spirit of the algo.

Sign up to request clarification or add additional context in comments.

2 Comments

can we group it again into result i mentioned above?
I've updated the answer, please take a look.

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.