1

How to get all the office values from this and store it as a simple array?

var data = {
  'XYZ': [{
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'open',
      timestamp: '02-12-2019 00:15:29'
    },
    {
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'processed',
      timestamp: '02-12-2019 00:15:32'
    }
  ],
  'yyy': [{
      office: 'yyy.in',
      reportName: 'payroll',
      event: 'delivered',
      timestamp: '02-12-2019 00:15:29'
    },
    {
      office: 'yyy.in',
      reportName: 'payroll',
      event: 'open',
      timestamp: '02-12-2019 00:15:32'
    }
  ],
  'zzz': [{
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'delivered',
      timestamp: '02-12-2019 00:15:29'
    },
    {
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'open',
      timestamp: '02-12-2019 00:15:32'
    }
  ]
}

The output should be like:

officesArray = ['xyz.in', 'yyy.in', 'xyz.in']

my code is

var Office = (Object.entries(grpDatas).flatMap(([k, v]) => (v.forEach(({office})=> office))))

It prints array of undefined.

3

3 Answers 3

2

Use map instead of forEach and also you're accessing email instead of office:

var data = {
  'XYZ': [{
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'open',
      timestamp: '02-12-2019 00:15:29'
    },
    {
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'processed',
      timestamp: '02-12-2019 00:15:32'
    }
  ],
  'yyy': [{
      office: 'yyy.in',
      reportName: 'payroll',
      event: 'delivered',
      timestamp: '02-12-2019 00:15:29'
    },
    {
      office: 'yyy.in',
      reportName: 'payroll',
      event: 'open',
      timestamp: '02-12-2019 00:15:32'
    }
  ],
  'zzz': [{
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'delivered',
      timestamp: '02-12-2019 00:15:29'
    },
    {
      office: 'xyz.in',
      reportName: 'payroll',
      event: 'open',
      timestamp: '02-12-2019 00:15:32'
    }
  ]
}

var Email = Object.values(data).flatMap(item => [...new Set(item.map(({office})=> office))])
console.log(Email)

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

6 Comments

i want the output in this way ['xyz.in','yyy.in','xyz.in'] i have used your way.
Actually, Object.entries isn't even necessary here, Object.values will be enough.
but it returns 2 times i want only time each value of the office.
@Amar then you need to use Set on the inner map to remove duplicates. I've updated the answer.
is it possible to filter the timestamp and get the timestamp according to that event and store it into the array ?
|
0

You could get unique values from the wanted properties.

var data = { XYZ: [{ office: 'xyz.in', reportName: 'payroll', event: 'open', timestamp: '02-12-2019 00:15:29' }, { office: 'xyz.in', reportName: 'payroll', event: 'processed', timestamp: '02-12-2019 00:15:32' }], yyy: [{ office: 'yyy.in', reportName: 'payroll', event: 'delivered', timestamp: '02-12-2019 00:15:29' }, { office: 'yyy.in', reportName: 'payroll', event: 'open', timestamp: '02-12-2019 00:15:32' }], zzz: [{ office: 'xyz.in', reportName: 'payroll', event: 'delivered', timestamp: '02-12-2019 00:15:29' }, { office: 'xyz.in', reportName: 'payroll', event: 'open', timestamp: '02-12-2019 00:15:32' }] },
    office = Object.values(data).flatMap(v => [...new Set(v.map(({ office }) => office))]);

console.log(office);

Comments

0

const result = Object.keys(data) .map( key => data[key]) .flat() .map( element => element.office)

Transforming everything to arrays and flattening them helps a lot!

https://codepen.io/t0dorakis/pen/MWYerKL

Comments

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.