split-object
Minimal tool for working with Objects using built-in functional Array methods.
split-object
will split an Object into an Array of keys & values,
allowing you to manipulate the Object using Array methods then join
values back into an Object.
In comparison to for..in
or Object.keys().forEach()
an Array of the
form [{key1: value1}, {key2: value2}]
can be a far more natural &
convenient structure to work with.
split-object
has both a split
and a join
method, similar to how
String#split
/Array#join
combine to convert back & forward between
Strings & Arrays.
Post ES6 we might see some better methods for iterating over Objects.
Installation
npm install split-object
Usage
var salad = apples: 1 bananas: 3 carrots: 2 // transform each key/value using Array.prototype.mapvar loudIngredients = // loudIngredients:// [// { key: 'APPLES', value: 3 },// { key: 'BANANAS', value: 9 },// { key: 'CARROTS', value: 6 }// ] var loudSalad = split // loudSalad:// {// APPLES: 3,// BANANAS: 9,// CARROTS: 6// }//
Examples
Iterating Keys & Values
// with object-split
// without object-splitObject // orfor var key in salad var value = saladkey console
Chaining Transformations
// with object-splitvar pieces = var newSalad = split
Without splitting the Object into a similar structure to split-object
, you're
stuck with losing the keys (which is acceptable if you can deduce keys from the
value) or using multiple reduce
calls/for..of
iteration:
// without object-splitvar newSalad = ObjectnewSalad = Object
split-object
doesn't save a huge number of lines, but it
saves some complexity, enables easier chaining and removes the hassle
of extracting the value from the object on each iteration.
Custom Key/Value Names
To provide more semantic key/value names, supply a second argument to
either split
or join
with the key/value mapping:
var salad = apples: 1 bananas: 3 carrots: 2 var loudIngredients = // loudIngredients:// [// { name: 'APPLES', amount: 3 },// { name: 'BANANAS', amount: 9 },// { name: 'CARROTS', amount: 6 }// ] var loudSalad = split // loudSalad:// {// APPLES: 3,// BANANAS: 9,// CARROTS: 6// }//
This also means you can use join
to create an Object out of any Array
of Objects with two properties.
Remember to also supply the mapping to join otherwise it won't be able to find the correct key/value pair to re-form the object.
See Also
- hughsk/flat – Flatten/unflatten nested Javascript objects (Highly recommended for use with
split-object
).
License
MIT