obj2paths

Convert a Javascript object into a list of leaf node paths and values

Usage no npm install needed!

<script type="module">
  import obj2paths from 'https://cdn.skypack.dev/obj2paths';
</script>

README

obj2paths

Convert a Javascript object into a list of leaf node paths and values

example

   const obj2paths = require('obj2paths');

   const paths = obj2paths({
     "an": "attribute",
     "a": {
       "deep": {
         "attribute": 42
       }
     },
     "also": {
       "works": {
         "for": {
           "arrays": [
            0,
            "a string",
            {
              "and": "objects"
            }
          ]
         }
       }
     }
   });

   console.log(JSON.stringify(paths, null, 2));

The above will log the following:

  [
    {
      "path": ["an"],
      "value": "attribute"
    },
    {
      "path": ["a", "deep", "attribute"],
      "value": 42
    },
    {
      "path": ["also", "works", "for", "arrays", 0],
      "value": 0
    },
    {
      "path": ["also", "works", "for", "arrays", 1],
      "value": "a string"
    },
    {
      "path": ["also", "works", "for", "arrays", 2, "and"],
      "value": "objects"
    }
  ]

what is it good for?

To build effient search indexes on objects, allow you to query collections of objects by any of their attributes.