JSPM

  • Created
  • Published
  • Downloads 725
  • Score
    100M100P100Q101404F
  • License ISC

data forest

Package Exports

  • d-forest

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d-forest) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Install

npm install d-forest

Usage

const df = require('d-forest');

const data = [{
    "id": 1,
    "name": "Leanne Graham",
    "address": {
        "street": "Kulas Light",
        "city": "Gwenborough",
        "zipcode": "92998-3874",
        "geo": {
            "lat": "-37.3159",
            "lng": "81.1496"
        }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
        "name": "Romaguera-Crona",
        "bs": "harness real-time e-markets"
    }
}, {
    "id": 2,
    "name": "Ervin Howell",
    "address": {
        "street": "Victor Plains",
        "city": "Wisokyburgh",
        "zipcode": "90566-7771",
        "geo": {
            "lat": "-43.9509",
            "lng": "-34.4618"
        }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
        "name": "Deckow-Crist",
        "bs": "synergize scalable supply-chains"
    }
}]

// "node" can be any object on tree
df(data).findNode(node => node.name === 'Deckow-Crist');
// {
//     name: 'Deckow-Crist',
//     bs: 'synergize scalable supply-chains'
// }

// "leaf" can be any object which don't have children
// this is useful when you know that the object you want to find has no children
// it has better performance over findNode as it skips the unnecessary comparisons
df(data).findLeaf(leaf => parseFloat(leaf.lng) > 0);
// {
//     lat: '-37.3159',
//     lng: '81.1496'
// }

df(data).mapLeaves(leaf => leaf.lat)
    .filter(value => value) // because value can be undefined
    .reduce((a, b) => a + Number(b), 0);
// -81.26679999999999