New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

return-tree-objects-path

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

return-tree-objects-path

A utility to recursively find paths in a list of objects

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

Return Tree Objects Path Library

The Return Tree Objects Path Library is a utility that allows you to recursively find paths in a list of objects based on a specified target key and value. It provides a simple and flexible way to traverse and search through complex object structures, enabling you to retrieve paths from the root to the target.

Installation

You can install the Return Tree Objects Path Library via npm. Make sure you have Node.js and npm installed. Then, run the following command:

npm install return-tree-objects-path
import { returnTreeObjectsPath } from 'return-tree-objects-path';

// Sample usage
const objects = [
  {
    id: 1,
    name: 'Parent',
    children: [
      {
        id: 2,
        name: 'Child 1',
        children: [],
      },
      {
        id: 3,
        name: 'Child 2',
        children: [
          {
            id: 4,
            name: 'Grandchild 1',
            children: [],
          },
        ],
      },
    ],
  },
];

const paths = returnTreeObjectsPath(objects, 'id', 4, 'children');
console.log(paths);
[
  { "id": 1, "name": "Parent" },
  { "id": 3, "name": "Child 2" },
  { "id": 4, "name": "Grandchild 1" }
]

API

returnTreeObjectsPath(objects, targetKey, targetValue, childrenProperty)

Recursively finds paths in a list of objects based on a specified target key and value.

  • objects: An array of objects to search through.
  • targetKey: The key to search for in the objects.
  • targetValue: The value to find in the objects based on the target key.
  • childrenProperty: The property name representing the children of each object.

Returns an array of objects representing the path from the root to the target.

Sample Usage

The Return Tree Objects Path Library can be useful for building breadcrumbs in a tree structure. Here's an example of how you can use the library to generate breadcrumbs from a hierarchical list of objects:

import { returnTreeObjectsPath } from 'return-tree-objects-path';

// Sample data representing a tree structure
const categories = [
  {
    id: 1,
    name: 'Home',
    children: [
      {
        id: 2,
        name: 'Products',
        children: [
          {
            id: 3,
            name: 'Laptops',
            children: [
              {
                id: 4,
                name: 'Gaming Laptops',
                children: [],
              },
              {
                id: 5,
                name: 'Business Laptops',
                children: [],
              },
            ],
          },
          {
            id: 6,
            name: 'Smartphones',
            children: [],
          },
        ],
      },
      {
        id: 7,
        name: 'About Us',
        children: [],
      },
    ],
  },
];

Contributing

We welcome contributions! If you find a bug or have a suggestion, please open an issue or create a pull request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Keywords

FAQs

Package last updated on 13 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc