0

I want to access a nested Json file, didn't figure out how to do it:

  var createPromise = new Promise(function (resolve, reject) {
        ddpclient.call('create_emailConnection',[ddpclient.collections.email], function (err, result) {
          console.log([ddpclient.collections.email])
          resolve();
          if (err) {
            logger.error(0, '❌ error user_created')
          } else {
            logger.debug(5, 'users marked as created')
          }
        })
      })

This return

email.json

{ HEDGz2aafs2UdSvT3S: 
     { _id: 'HEDGz2aafs2UdSvT3S',
       batch: '2017-user-created-by-websocket',
       creationIP: 'None',
       creationDate: 2018-06-14T13:23:58.786Z,
       state: [Object],
    },
    M101KSi360g7cER8D5: 
     { _id: 'M101KSi360g7cER8D5',
       batch: '2017-box-created-by-websocket',
       creationIP: 'None',
       creationDate: 2018-06-14T13:24:31.145Z,
       state: [Object],
      } } 

So I want to get the _id on my json file

EyTa
  • 109
  • 3
  • 10
  • Side note: Don't resolve the promise on error, that hides the fact an error occurred. Reject on error, that's what promise rejection is for. – T.J. Crowder Jun 15 '18 at 07:21

3 Answers3

0

Since, the object keys HEDGz2aafs2UdSvT3S, M101KSi360g7cER8D5 looks random and unpredictable so you can use Object.keys() on the array object and loop through them to get the value of _id.

var arr = [ { HEDGz2aafs2UdSvT3S: 
     { _id: 'HEDGz2aafs2UdSvT3S',
       batch: '2017-user-created-by-websocket',
       creationIP: 'None',
       creationDate: '2018-06-14T13:23:58.786Z'
    },
    M101KSi360g7cER8D5: 
     { _id: 'M101KSi360g7cER8D5',
       batch: '2017-box-created-by-websocket',
       creationIP: 'None',
       creationDate: '2018-06-14T13:24:31.145Z'
      } } ];
      
arr.forEach((obj)=>{
  keys = Object.keys(obj);
  keys.forEach((key)=>{
   console.log(obj[key]._id);
  });
});
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Use can use Array.reduce and Object.keys

You have an array of objects with each object having multiple objects with each being mapped to _id being key of object. Now, you can reduce the array to concatenate all the _ids. As the key of object and _id are same, you can use the key of object to extract the _id.

var arr = [{ HEDGz2aafs2UdSvT3S: { _id: 'HEDGz2aafs2UdSvT3S',batch: '2017-user-created-by-websocket',creationIP: 'None',creationDate: '2018-06-14T13:23:58.786Z'},M101KSi360g7cER8D5:{ _id: 'M101KSi360g7cER8D5',batch: '2017-box-created-by-websocket',creationIP: 'None',creationDate: '2018-06-14T13:24:31.145Z'} } ];
let ids = arr.reduce((a,c) => [...a, ...Object.keys(c)], []);
console.log(ids);

EDIT

let obj = { HEDGz2aafs2UdSvT3S: { _id: 'HEDGz2aafs2UdSvT3S',batch: '2017-user-created-by-websocket',creationIP: 'None',creationDate: '2018-06-14T13:23:58.786Z'},M101KSi360g7cER8D5:{ _id: 'M101KSi360g7cER8D5',batch: '2017-box-created-by-websocket',creationIP: 'None',creationDate: '2018-06-14T13:24:31.145Z'} };
let ids = Object.keys(obj);
console.log(ids);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • I did let ids = ddpclient.collections.email.reduce((a,c) => [...a, ...Object.keys(c)], []); console.log(ids) it tell me reduce is not a function – EyTa Jun 15 '18 at 07:31
  • @EyTa - Is the above email.json stored in `ddpclient.collections.email` ? – Nikhil Aggarwal Jun 15 '18 at 07:36
  • It's a call to the database my json is like this sorry not an array i'll modify my question one second – EyTa Jun 15 '18 at 07:37
  • var arr = { HEDGz2aafs2UdSvT3S: { _id: 'HEDGz2aafs2UdSvT3S',batch: '2017-user-created-by-websocket',creationIP: 'None',creationDate: '2018-06-14T13:23:58.786Z'},M101KSi360g7cER8D5:{ _id: 'M101KSi360g7cER8D5',batch: '2017-box-created-by-websocket',creationIP: 'None',creationDate: '2018-06-14T13:24:31.145Z'} } – EyTa Jun 15 '18 at 07:39
  • @EyTa - See my updated answer. You just need `Object.keys` – Nikhil Aggarwal Jun 15 '18 at 07:42
0

Try this:

a.map(x => Object.keys(x).map(y => x[y]._id))

Which will return you an array of the properties _id.

Fiddle: https://jsfiddle.net/ujhfwz0g/3/

Phil Cooper
  • 3,083
  • 39
  • 63