Skip to content

Instantly share code, notes, and snippets.

@spoike
Created May 6, 2014 14:45
Show Gist options
  • Select an option

  • Save spoike/0fb3d40bfaef366c00a9 to your computer and use it in GitHub Desktop.

Select an option

Save spoike/0fb3d40bfaef366c00a9 to your computer and use it in GitHub Desktop.
Handling irregular objects in an array, where inspecting chain may lead to TypeErrors due to undefined values. Useful on REST API's that for some reason returns irregular objects in an array.
// Adds pluckRef method to lodash
// Example: _.(array, 'key1.key2.key3');
// Goes through each object of the array and attempts to pluck objects that are
// in the specified object chain path. It filters away undefined values.
_.mixin({
'pluckRef': function(arr, keyPathRef) {
var i, j, keys, key, ref, output = [];
keys = keyPathRef.split('.');
// for each object in the array
for (i = 0; i < arr.length; i++) {
ref = arr[i];
// for each key in keyPathRef
for (j = 0; j < keys.length; j++) {
key = keys[j];
// reduce reference to the next key
ref = ref[key];
// if undefined value is hit, break the loop
if (typeof ref === 'undefined') {
break;
}
}
// if undefined continue to the next
if (typeof ref === 'undefined') {
continue;
}
// else valid value is pushed to the output
output.push(ref);
}
return output;
}
});

Testcase

An array of cats and dogs

var catsAndDogsArr = [ 
  { 
    cat: { 
      name: 'Arya' 
    } 
  },
  { 
    cat: { 
      name: 'Ser Pounce' 
    } 
  }, 
  { 
    dog: { 
      name: 'The Hound' 
    } 
  } 
];

Usage

Use it in lodash as usual:

_.pluckRef(catsAndDogsArr, 'cat.name');
// -> ["Arya", "Ser Pounce"]
_.pluckRef(catsAndDogsArr, 'dog.name');
// -> ["The Hound"]

Or use lodash's chaining facility if you need to handle the data coming out of pluckRef:

_(catsAndDogsArr).pluckRef('cat.name').value();
// -> ["Arya", "Ser Pounce"]
_(catsAndDogsArr).pluckRef('dog.name').value();
// -> ["The Hound"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment