-1

I am trying to create some script which will take a Object and then return a list of all keys in that Object which works for two level keys. But once the key has another object or the Key is an Array holding Objects the script no longer returns the keys. Is there a simple way to do this or will I have to manually loop thru all keys and check if there is another object? Below is a sample object. In my case I don't care about the values but I need all keys:

object1 = {
            DocId: "email_campaign::3ed76589-4063-49f6-a21e-9ca16981d102",
            _id: "3ed76589-4063-49f6-a21e-9ca16981d102",
            _type :"email_campaign",
            end_date: "",
            history: {
                created_by: "",
                created_on: "",
                update_on: "",
                updated_by: ""
            },
            librarys :[{id: 1, name : 'Lib 1'},{ id: 2, name: 'Lib 2'}],
            metrics : {
                first_email_sent: "",
                last_email_send: "",
                nbr_of_attachments_opened: 0,
                nbr_of_bounces: 0,
                nbr_of_email_opened: 0,
                nbr_of_emails: 0,
                nbr_of_unique_attachments_opened:0,
                nbr_of_unique_email_opened: 0
            },
            start_date: "",
            status: "Active",
            subject: "Test 1 Subject",
            summary: "",
            tags: ['one', 'two'],
            template_id: ""
        };
MisterniceGuy
  • 1,646
  • 2
  • 18
  • 41
  • Can you clarify this? "which works for to level keys" – Geuis Jan 17 '20 at 21:36
  • You may want to post what code you have for getting keys currently, but my guess is that you'll want to turn it into a recursive function that can call itself whenever it finds another level deep. – willman Jan 17 '20 at 21:37
  • so if i use lodash / underscore and this simple code let myKeys = _.allKeys(obj) i get for example metrics but not the keys under metrics – MisterniceGuy Jan 17 '20 at 21:47
  • In your example, I see nowhere that "the key has another object or the Key is an Array holding Objects". I see places where the *value* is an object, and where the *value* is an array of objects, but that's different. – Heretic Monkey Jan 17 '20 at 22:06
  • maybe i interchanged terms but for example metrics is a key in the object and has a bunch of nested keys or the librarys key has an array of another object which has 2 keys – MisterniceGuy Jan 17 '20 at 22:17
  • https://stackoverflow.com/questions/47062922/how-to-get-all-keys-with-values-from-nested-objects answers some part as it handles keys like history and metrics but not the librarys array – MisterniceGuy Jan 17 '20 at 22:23
  • Use answers from [Javascript - Traverse through the Json Object and get each item hierarchical key including nested objects and arrays](https://stackoverflow.com/q/59134046/215552) – Heretic Monkey Jan 17 '20 at 22:46

2 Answers2

0

The following code might help you. This gives you an array of key value. You can tweak it as per your use case.

const object1 = {
  DocId: "email_campaign::3ed76589-4063-49f6-a21e-9ca16981d102",
  _id: "3ed76589-4063-49f6-a21e-9ca16981d102",
  _type :"email_campaign",
  end_date: "",
  history: {
      created_by: "",
      created_on: "",
      update_on: "",
      updated_by: ""
  },
  librarys : [{id: 1, name : 'Lib 1', id: 2, name: 'Lib 2'}],
  metrics : {
      first_email_sent: "",
      last_email_send: "",
      nbr_of_attachments_opened: 0,
      nbr_of_bounces: 0,
      nbr_of_email_opened: 0,
      nbr_of_emails: 0,
      nbr_of_unique_attachments_opened:0,
      nbr_of_unique_email_opened: 0
  },
  start_date: "",
  status: "Active",
  subject: "Test 1 Subject",
  summary: "",
  tags: ['one', 'two'],
  template_id: ""
};

function getKeyValues(obj, path = [], result = []) {
  let value;
  let fieldPath;
  Object.keys(obj).forEach(key => {
    value = obj[key];
    const modifiedKey = isNaN(key) ? key : `[${key}]`;
    fieldPath = path.concat([modifiedKey]);
    if (value instanceof Object) {
      getKeyValues(value, fieldPath, result);
    }
    result.push({"key": fieldPath.join("."), value});
  });
  return result;
}

console.log(getKeyValues(object1));
Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
0

Object.keys() ?

const object1 = {
  DocId: "email_campaign::3ed76589-4063-49f6-a21e-9ca16981d102",
  _id: "3ed76589-4063-49f6-a21e-9ca16981d102",
  _type :"email_campaign",
  end_date: "",
  history: {
      created_by: "",
      created_on: "",
      update_on: "",
      updated_by: ""
  },
  librarys : [{id: 1, name : 'Lib 1', id: 2, name: 'Lib 2'}],
  metrics : {
      first_email_sent: "",
      last_email_send: "",
      nbr_of_attachments_opened: 0,
      nbr_of_bounces: 0,
      nbr_of_email_opened: 0,
      nbr_of_emails: 0,
      nbr_of_unique_attachments_opened:0,
      nbr_of_unique_email_opened: 0
  },
  start_date: "",
  status: "Active",
  subject: "Test 1 Subject",
  summary: "",
  tags: ['one', 'two'],
  template_id: ""
};

console.log(Object.keys(object1));
Wilhelmina Lohan
  • 2,803
  • 2
  • 29
  • 58