Is there a reason why Object.keys(myObj).length wo...
# suitescript
k
Is there a reason why Object.keys(myObj).length would throw
Cannot read property 'length' of undefined
for an object that has keys?
l
Can you share with us the value of myObj ?
k
["6", "27290", "37071"]
oh wait that's the value of Object.keys(myObj)
b
if Object.keys(myObj) is ["6", "27290", "37071"]
its length should be 3
l
Yes.
k
{6: Array(4), 27290: Array(2), 37071: Array(1)}
maybe it doesn't like numbers as properties
l
question. Are you sure you are passing an object and not a stringified JSON ? Numbers should work fine.
b
it shouldnt care unless you are doing weird stuff with prototypes or non enumerable keys
l
Also, are you using Suitescript 2.0 or 2.1 ? I don't know for 2.0, but I used it in 2.1 this morning, without any problems.
k
2.x
s
maybe you can just use lodash for whatever you intended
d
make sure
myObj
is accessible? because
Object.keys({}).length
will return you
0
so theres no reason Object.keys() is faulty in 2.x
k
I figured it out. I was doing it in a for of using typescript
changed it to for in
and it started working
the error was actually from the compiled javascript of the for loop
not the Object.keys().length
s
you could use lodash instead of 'for of' or 'for in'
or <insert your functional library of choice here>
i love new features, but kinda feel it's too easy to mistake 'for of' and 'for in', and a functional library will have methods that better describe the semantics of what one is trying to do with the iteration
s
Why would load in lodash to do a simple for in loop, seems like overkill
s
imho, lodash should always be there. writing yet-another-loop by hand is overkill.
I can't even remember the last time I wrote a loop by hand.
..and by not writing them, I can't make mistakes with them.
so yeah, not overkill to me.