“_.Get Alternative” Code-Antworten

_.Get Alternative


if (fgets(buf, sizeof buf, fp)) {
    buf[strcspn(buf, "\n")] = '\0';
}

Embarrassed Emu

_.Get Alternative

//This Is my alternative to (lodash) _.get() method 

const get = (obj, path, def) => (() => typeof path === 'string' ? path.replace(/\[(\d+)]/g,'.$1') : path.join('.'))()
  .split('.')
  .filter(Boolean)
  .every(step => ((obj = obj[step]) !== undefined)) ? obj : def



//Usages Example
var prop = get(data, '[0].items[0].property', 'backupValue');

//returns found value of path 
Naval

Ähnliche Antworten wie “_.Get Alternative”

Fragen ähnlich wie “_.Get Alternative”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen