Moment JS

Example of parsing and formatting a Date with Moment JS.

by Aswin Devarajan

HTML

<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

JavaScript

let stringVal = "first.second.third.fourth";
console.log(stringVal.split('.'))
let viva = {}; 

let tiva = stringVal.split('.');



const getNestedObject = (nestedObj, pathArr) => {
    return pathArr.reduce((obj, key) =>
        (obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObj);
}

// pass in your object structure as array elements
const name = getNestedObject(viva, tiva);
console.log(name)

// to access nested array, just pass in array index as an element the path array.
/* const city = getNestedObject(user, ['personalInfo', 'addresses', 0, 'city']); */
// this will return the city from the first address item.
/* let vida = (vida || {}) */