npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

split-object

v2.1.1

Published

Work with Objects using built-in functional Array methods

Downloads

50

Readme

split-object

Build Status

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.map
var loudIngredients = split(salad).map(function(ingredient) {
  ingredient.key = ingredient.key.toUpperCase()
  ingredient.value *= 3
  return ingredient
})

// loudIngredients:
// [
//   { key: 'APPLES', value: 3 },
//   { key: 'BANANAS', value: 9 },
//   { key: 'CARROTS', value: 6 }
// ]

var loudSalad = split.join(loudIngredients)

// loudSalad:
// {
//    APPLES: 3,
//    BANANAS: 9,
//    CARROTS: 6
// }
//

Examples

Iterating Keys & Values

// with object-split
split(salad).forEach(function(item) {
  console.log(item.key, item.value)
})
// without object-split
Object.keys(salad).forEach(function(key) {
  var value = salad[key]
  console.log(key, value)
})

// or
for (var key in salad) {
  var value = salad[key]
  console.log(key, value)
}

Chaining Transformations

// with object-split
var pieces = split(salad)
.map(function(kv) {
  kv.value = calculate(kv.value)
  return kv
})
.map(function(kv) {
  kv.value = recalculate(kv.value)
  return kv
})
var newSalad = split.join(pieces)

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-split
var newSalad = Object.keys(salad)
.reduce(function(obj, key) {
  var value = salad[key]
  obj[key] = calculate(value)
  return obj
}, {})
newSalad = Object.keys(newSalad)
.reduce(function(obj, key) {
  var value = newSalad[key]
  obj[key] = recalculate(value)
  return obj
}, {})

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 = split(salad, {key: 'name', value: 'amount'}).map(function(ingredient) {
  ingredient.name = ingredient.name.toUpperCase()
  ingredient.amount *= 3
  return ingredient
})

// loudIngredients:
// [
//   { name: 'APPLES', amount: 3 },
//   { name: 'BANANAS', amount: 9 },
//   { name: 'CARROTS', amount: 6 }
// ]

var loudSalad = split.join(loudIngredients, {key: 'name', value: 'amount'})

// 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