re: A little tip on running functions on JS objects VIEW POST


You could use the lodash/get method to retrieve the value, then call .length on that.

import get from 'lodash/get';

const obj = {
  foo: [1,3,8,2,1],
  bar: [
             yah: yeet,
             yeezy: [1,8,9]

get(obj, 'foo').length // 5
get(obj, 'bar[1].yeezy').length // // 3

I think I prefer this syntax more.

Another thing that is great about this method is if the value you're trying to get doesn't exist, it allows you to define a value to return anyways, like so:

const obj = {
  foo: 'bar'

get(obj, 'bar', 'No bar'); // 'No bar'
code of conduct - report abuse