Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                console.clear();

const name1 = "foo.bar"; // Should return { baz: 42 }
const name2 = "foo.bar.baz"; // Should return 42
const name3 = "davis"; // Should return 'ford'
const name4 = "foo.going.even.deeper.now"; // Should return 0
const name5 = "foo.bar.invalid_path"; // Should return undefined

const testNames = [name1, name2, name3, name4, name5];

const obj = {
  davis: "ford",
  foo: {
    bar: {
      baz: 42
    },
    going: {
      even: {
        deeper: {
          now: 0
        }
      }
    }
  }
};

/**
 * Solution 1: The one I wrote
 */
const getKeyValue = (obj, key) => {
  // Best case scenario: The key is a top level key
  if (obj.hasOwnProperty(key)) return obj[key];

  // If there's no keys in the object, don't bother
  if (Object.keys(obj).length === 0) return undefined;

  // Split the name into keys
  const keys = key.split(".");

  // Reduce the keys
  const { value } = keys.reduce(
    (accum, k, i) => {
      // Don't process any more if we've already set break to true
      if (accum.break) return accum;

      // If the last key exists in the object, we've found our answer!
      if (i === keys.length - 1 && accum.obj.hasOwnProperty(k)) {
        accum.value = accum.obj[k];
        return accum;
      }

      // Does the obj have this key? If so, keep drilling down
      if (accum.obj.hasOwnProperty(k)) {
        accum.obj = accum.obj[k];
        return accum;
      }

      // If we can't find the key, we've been given an invalid path
      // So set `break` to true to stop processing
      accum.break = true;
      return accum;
    },
    {
      break: false,
      value: undefined,
      obj
    }
  );

  return value;
};

const start1 = performance.now();
console.log(testNames.forEach((key) => getKeyValue(obj, key)));
const end1 = performance.now();
const perf1 = end1 - start1;

/**
 * Co-worker's suggestion
 */
const getKeyValue2 = (obj, path) => {
  const keys = path.split(".");
  while (keys.length) {
    let loc = keys.shift();
    if (obj.hasOwnProperty(loc)) {
      obj = obj[loc];
    } else {
      obj = undefined;
      break;
    }
  }
  return obj;
};

const start2 = performance.now();
console.log(testNames.forEach((key) => getKeyValue2(obj, key)));
const end2 = performance.now();
const perf2 = end2 - start2;

console.log({
  perf1,
  perf2,
  diff: perf1 > perf2
});

              
            
!
999px

Console