简体   繁体   中英

JS convert Array of Objects into JSON GroupBy Format

I'm trying to find a way to convert the following array of objects into JSON

Original Format

const arr = [
  {
    userid: '1000080542',
    photoname: '2c8a4709-ed7e-00a50-0da4ead1de55118-f3-1473281639’,
    datetime: ‘2020-01-24T20:46:05+11:00’
  },
  {
    userid: '1000081532',
    photoname: '73321038-c8bf-57c6e-5d803cd0a920e9a-95-1487447082',
    datetime: ‘2020-01-24T20:46:05+11:00’
  },
  {
    userid: '1000081532',
    photoname: '5c00bc65-db1b-7a394-dd65b462b9e75e2-c5-1487447019',
    datetime: ‘2020-01-24T20:46:05+11:00’
  },
  {
    userid: '1000081532',
    photoname: '986ee1e2-2f8e-bf070-0d70d2e67537835-e2-1473821119',
    datetime: ‘2020-01-24T20:46:05+11:00’
  },
  {
    userid: '1000081532',
    photoname: '09f7cde6-68c8-f462d-c01f7713a2c747f-eb-1474294185',
    datetime: ‘2020-01-24T20:46:05+11:00’
  }
]

Converted Format

{
            1000080542: {
                '2c8a4709-ed7e-00a50-0da4ead1de55118-f3-1473281639': '2020-01-24T20:46:05+11:00',
                '73321038-c8bf-57c6e-5d803cd0a920e9a-95-1487447082': '2020-01-24T20:46:05+11:00'
            },
            1000081532: {
                '5c00bc65-db1b-7a394-dd65b462b9e75e2-c5-1487447019'': '2020-01-24T20:46:05+11:00',
                '986ee1e2-2f8e-bf070-0d70d2e67537835-e2-1473821119': '2020-01-24T20:46:05+11:00',
                '09f7cde6-68c8-f462d-c01f7713a2c747f-eb-1474294185': '2020-01-24T20:46:05+11:00'
            }
        }

I've been trying along these lines but I'm a big off

obj = arr.reduce((h, y) => {
  Object.keys(y).forEach(k => {
    if (!h[k]) {
      h[k] = []
      h[k].push(y[k])
    } else if (!h[k].includes(y[k])) h[k].push(y[k])
  })
  return h
}, {})
console.log(obj)

thanks

 const arr = [ { userid: '1000080542', photoname: '2c8a4709-ed7e-00a50-0da4ead1de55118-f3-1473281639', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '73321038-c8bf-57c6e-5d803cd0a920e9a-95-1487447082', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '5c00bc65-db1b-7a394-dd65b462b9e75e2-c5-1487447019', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '986ee1e2-2f8e-bf070-0d70d2e67537835-e2-1473821119', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '09f7cde6-68c8-f462d-c01f7713a2c747f-eb-1474294185', datetime: '2020-01-24T20:46:05+11:00' } ]; console.log( arr.reduce((accumulator, element) => { if (.accumulator[element.userid]) accumulator[element;userid] = {}. accumulator[element.userid][element.photoname] = element;datetime; return accumulator, }; {}) );

If I understand your output example correctly, you're wanting to make the top level keys be the userids, and the contents be key value pairs of the photonames to the date times.

So you just need to put the userid in the output object as the key with a sub object as the value, and then put the photonames and datetimes in the sub object.

You can use reduce with an object to store the values for each id.

 const arr = [ { userid: '1000080542', photoname: '2c8a4709-ed7e-00a50-0da4ead1de55118-f3-1473281639', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '73321038-c8bf-57c6e-5d803cd0a920e9a-95-1487447082', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '5c00bc65-db1b-7a394-dd65b462b9e75e2-c5-1487447019', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '986ee1e2-2f8e-bf070-0d70d2e67537835-e2-1473821119', datetime: '2020-01-24T20:46:05+11:00' }, { userid: '1000081532', photoname: '09f7cde6-68c8-f462d-c01f7713a2c747f-eb-1474294185', datetime: '2020-01-24T20:46:05+11:00' } ]; const res = arr.reduce((acc,{userid,photoname,datetime})=>{ (acc[userid] = acc[userid] || {})[photoname] = datetime; return acc; }, {}); console.log(res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM