You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

js-helper-functions

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-helper-functions

---

2.0.9
Source
npmnpm
Version published
Weekly downloads
10
-68.75%
Maintainers
1
Weekly downloads
 
Created
Source

Javascript Utility Functions

TypeScript utility functions.

countItemsInMultidimensionalObject

A function that recursively counts the number of selected items in a nested data structure. This function is particularly useful when dealing with hierarchical selection states such as those found in tree views, nested checklists, or multi-level category pickers.

✨ Features
  • Handles both arrays and deeply nested objects.
  • Returns a detailed breakdown of item counts per level.
  • Provides a total count of all selected items.
Data Type
type NestedObject = {
  [key: string]: Node | string[];
};
Parameeter
NameTypeDescription
dataNestedObject Or string[]A nested object or array representing selected items.

📤 Returns

  • If the input is a flat array, it returns a number representing the count of items.
  • If the input is a nested object, it returns an object of type SelectionCountsResult:
{
  total: number;     // Total count of items in the entire structure
  result: {
    [key: string]: number | { ...nested_object }
  }
}
Example
const selection = {
  fruits: ["apple", "banana"],
  vegetables: {
    root: ["carrot", "beet"],
    leafy: ["spinach"],
  },
  grains: {
    rice: ["basmati"],
    wheat: [],
  },
};

const result = countItemsInMultidimensionalObject(selection);
console.log(result);
result:
{
  total: 6,
  result: {
    fruits: 2,
    vegetables: {
      root: 2,
      leafy: 1
    },
    grains: {
      rice: 1,
      wheat: 0
    }
  }
}

Keywords

react

FAQs

Package last updated on 22 May 2025

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