```{ "results": { "users": { "933849":...
# suitescript
s
Copy code
{
  "results": {
    "users": {
      "933845": {
        "id": 933845,
        "first_name": "Bob",
        "last_name": "Smith",
        "group_id": 64965,
        "active": true,
        "employee_number": 0,
        "salaried": false,
        "exempt": false,
        "username": "bobsmith",
        "email": "",
        "email_verified": false,
        "payroll_id": "",
        "hire_date": "0000-00-00",
        "term_date": "0000-00-00",
        "last_modified": "2018-03-27T16:13:33+00:00",
        "last_active": "2018-03-28T20:16:39+00:00",
        "created": "2018-03-27T16:13:33+00:00",
        "client_url": "api_sample_output",
        "company_name": "API Sample Output Company",
        "profile_image_url": "",
        "display_name": "",
        "pronouns": "",
        "mobile_number": "",
        "pto_balances": {
          "2624351": 0,
          "2624353": 0,
          "2624355": 0
        },
        "submitted_to": "2000-01-01",
        "approved_to": "2000-01-01",
        "manager_of_group_ids": [],
        "require_password_change": false,
        "pay_rate": 0,
        "pay_interval": "hour",
        "permissions": {
          "admin": false,
          "mobile": true,
          "status_box": false,
          "reports": false,
          "manage_timesheets": false,
          "manage_authorization": false,
          "manage_users": false,
          "manage_my_timesheets": false,
          "manage_jobcodes": false,
          "pin_login": false,
          "approve_timesheets": false,
          "manage_schedules": false,
          "external_access": false,
          "manage_my_schedule": false,
          "manage_company_schedules": false,
          "view_company_schedules": false,
          "view_group_schedules": false,
          "manage_no_schedules": false,
          "view_my_schedules": false,
          "time_tracking": false
        },
        "customfields": ""
      },
      "933849": {
        "id": 933849,
        "first_name": "Mary",
        "last_name": "Samsonite",
        "group_id": 0,
        "active": true,
        "employee_number": 0,
        "salaried": false,
        "exempt": false,
        "username": "admin",
        "email": "<mailto:admin@example.com|admin@example.com>",
        "email_verified": false,
        "payroll_id": "",
        "mobile_number": "2087231456",
        "hire_date": "0000-00-00",
        "term_date": "0000-00-00",
        "last_modified": "2018-03-28T17:24:20+00:00",
        "last_active": "",
        "created": "2018-03-27T16:13:34+00:00",
        "client_url": "api_sample_output",
        "company_name": "API Sample Output Company",
        "profile_image_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61",
        "display_name": "",
        "pronouns": "",
        "pto_balances": {
          "2624351": 0,
          "2624353": 0,
          "2624355": 0
        },
        "submitted_to": "2000-01-01",
        "approved_to": "2000-01-01",
        "manager_of_group_ids": [],
        "require_password_change": false,
        "pay_rate": 0,
        "pay_interval": "hour",
        "permissions": {
          "admin": true,
          "mobile": true,
          "status_box": false,
          "reports": false,
          "manage_timesheets": false,
          "manage_authorization": false,
          "manage_users": false,
          "manage_my_timesheets": false,
          "manage_jobcodes": false,
          "pin_login": false,
          "approve_timesheets": false,
          "manage_schedules": false,
          "external_access": false,
          "manage_my_schedule": false,
          "manage_company_schedules": false,
          "view_company_schedules": false,
          "view_group_schedules": false,
          "manage_no_schedules": false,
          "view_my_schedules": false,
          "time_tracking": false
        },
        "customfields": ""
      }
    }
  },
  "more": false,
  "supplemental_data": {
    "jobcodes": {
      "2624351": {
        "id": 2624351,
        "parent_id": 0,
        "assigned_to_all": true,
        "billable": false,
        "active": true,
        "type": "pto",
        "has_children": false,
        "billable_rate": 0,
        "short_code": "",
        "name": "Sick",
        "last_modified": "2018-03-27T16:13:28+00:00",
        "created": "2018-03-27T16:13:28+00:00",
        "filtered_customfielditems": "",
        "required_customfields": [],
        "locations": []
      },
      "2624353": {
        "id": 2624353,
        "parent_id": 0,
        "assigned_to_all": true,
        "billable": false,
        "active": true,
        "type": "pto",
        "has_children": false,
        "billable_rate": 0,
        "short_code": "",
        "name": "Vacation",
        "last_modified": "2018-03-27T16:13:28+00:00",
        "created": "2018-03-27T16:13:28+00:00",
        "filtered_customfielditems": "",
        "required_customfields": [],
        "locations": []
      },
      "2624355": {
        "id": 2624355,
        "parent_id": 0,
        "assigned_to_all": true,
        "billable": false,
        "active": true,
        "type": "pto",
        "has_children": false,
        "billable_rate": 0,
        "short_code": "",
        "name": "Holiday",
        "last_modified": "2018-03-27T16:13:28+00:00",
        "created": "2018-03-27T16:13:28+00:00",
        "filtered_customfielditems": "",
        "required_customfields": [],
        "locations": []
      }
    },
    "groups": {
      "64965": {
        "id": 64965,
        "active": true,
        "name": "Construction",
        "last_modified": "2018-03-27T16:13:30+00:00",
        "created": "2018-03-27T16:13:29+00:00",
        "manager_ids": [
          "933833"
        ]
      }
    }
  }
}
m
s
I tried that but for some reason it only returns the first index
m
Share your code and what result you are expecting
s
Copy code
for (var data in body.results.timesheets) {
                    log.debug('body.results.timesheets[data],',body.results.timesheets[data]);
}
I'm trying to get ids and emails so I can process them individually
m
Your object doesn't have a
timesheets
key. Did you mean
body.results.users
?
s
I would suggest using lodash library to extract values you want if possible
m
Lodash is a great library but I think a bit of an overkill for just that
s
@michoel you are right! I had been coding for 10 hours straight yesterday and I ended up my mixing a couple scripts lol
users is what I needed
Thanks for pointing out my mistake 🙂 appreciate it
s
lodash is such a great library I consider it part of JS itself. In most cases there is no reasonable argument against using a widely battle tested and unit tested library over writing your own code.
s
I am definitely going to try lodash as well
What's the best place to learn Lodash though? I have never used it before.
s
the lodash documentation is actually pretty good - it shows an example of every function in the library.
s
sweet thanks!
s
just start reading at lodash.com
🙏 1
s
quick question: is "body.results.users.length" the right way of getting the length of an object?
s
another strong reason to use it (imho) is that the names of the functions are well known. So it means another developer is much more likely to follow along vs. the same functionality written without the library
s
makes sense - thanks
s
if
users
is an array, it will have a
length
property, if that's what you're asking.
s
yeah but this object is not an array. users is an object inside an object.
s
you can use Object.keys(users).length or with lodash _.keys(users)
however, one thing you'll learn from libraries like lodash is you very rarely create loops and iterate on your own in well written code.
with lodash, instead of a generic loop, you'd pick a function with an appropriate name that does what you were writing a loop to do in the first place.
just as a point of reference, I can't even remember the last time I wrote a
for
loop in JS
probably the most commonly used functions for this in lodash are
map
,
filter
,
forEach
,
reduce
many similar functions are available natively on the Array type in newer JS versions but lodash lets you have consistent code across JS runtimes. Also supports functional style but that's a topic for another day.
s
Thank you so much for your help. I'm reading through the documentation and I can't wait to try it
s
far too many times I've had to reverse-engineer hand written loops only to realize oh, the developer is trying to do
filter
and
map
here
the mental model I like to use to explain this is that
filter
and
map
describe WHAT you are doing (filter and map have meaning) whereas a hand written loops tells HOW you are doing something without saying anything useful about WHAT you're doing.
👍 1
for example, you have to reverse engineer a hand written loop in order to realize it is just `map`ing elements of a collection. In such case I argue that
map
would be both easier to understand and less buggy (any handwritten loop introduces an opportunity for new bugs)
anyway, I'll quiet down now. I've just had to unwrap way to many unnecessary deeply nested loops in my career.
😜 1
s
Haha - you clearly love lodash and I really am thankful for your insight on lodash
s
it's not so much lodash that I love as what it represents. pick another similar library and I'd be just as happy, though lodash does include a LOT of value across a wide range of use cases. Ramda or ImmutableJS or RxJs would all be similarly useful candidates for this stuff - as they are unit tested and generally use the same names for these high level operations so the meaning is still there and recognizable.
map()
tends to have the same meaning across most libraries, which is probably why even the native JS Array chose
map()
as a name for what
Array.map()
does 🙂
🙌 2
one other note I can leave you with is lodash has a looser contract with regards to things it iterates over, hence making it more flexible than the equivalent Array.XXX functions (which only work strictly on Array objects)
🙏 2