javascript - lodash find matching keys/nested keys -


i have standard object, nested objects.

i trying find keys contain 'gmt' , format corresponding epoch value via moment.

var myobject = {     "suffix" : "mr",     "fname" : "jullian",     "lname" : "exor",     "dobgmt" : 145754294700000     "addressline1" : "flat 8a",     "street" : "hoxley close",     "rentstartedgmt" : 145754294700000,     "deposit" : "50.00",     "occupation" : "math teacher",     "profession" : {          "careerstartedgmt": 1458755224800000,          "careerendgmt": 1459854224800000,      },     "salary" : 28000,     "votingdetail" : {         "location" : "virgina",         "votedongmt": 1874585224800000,         "votedfor" : "world wildlife foundation"     } } 

i can use standard js loop through keys above (see below), find rentstartedgmt miss profession.careerstartedgmt, profession.careerendgmt , votingdetail.votedongmt

var myobjectclone = _.clone(myobject);  (var key in myobjectclone) {     if (key.indexof("gmt") !== -1) {          var timevalue = myobjectclone[key];          timevalue = timevalue.format('dd-mm-yy hh:mm:ss');      } } 

i using lodash, there way can find keys contain 'gmt', modify epoch, , return object clone.


update: using recursion:

function findgmt(data) {     (var key in data) {         var v = data[key];         if (key.indexof("gmt") !== -1) {           }          if(v && typeof v === "object") {             findgmt(v);         }     } }  findgmt(myobjectclone); 

don't know of lodash specific method this...

but here's recursive function in vanilla js:

var obj = {    "suffix": "mr",    "fname": "jullian",    "lname": "exor",    "dobgmt": 145754294700000,    "addressline1": "flat 8a",    "street": "hoxley close",    "rentstartedgmt": 145754294700000,    "deposit": "50.00",    "occupation": "math teacher",    "profession": {      "careerstartedgmt": 1458755224800000,      "careerendgmt": 1459854224800000,    },    "salary": 28000,    "votingdetail": {      "location": "virgina",      "votedongmt": 1874585224800000,      "votedfor": "world wildlife foundation"    }  }      function flatk(o) {    return object.keys(o).reduce(function(ac, x) {      if (typeof o[x] === 'object')        ac.push(flatk(o[x]).join());      else        ac.push(x);      return ac    }, [])  }    console.log(flatk(obj))

then can filter output looking 'gmt' in key (but i'll let )


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -