Home > Enterprise >  group and array of object data javascript
group and array of object data javascript

Time:11-17

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

CodePudding user response:

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.

  • Related