Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-batcher2

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-batcher2

Batches items in an array

  • 1.0.0
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Array Batcher

A method for batching items in an array, returning a new Array containing smaller Arrays of batched items.

Items are batched in order, with the first batch containing the first items in the original Array.

A batchSize parameter needs to be provided to specify the max size of the batches to be created. Batches will be created to this max size, up until the final batch which will be filled partially if there are not enough items to fill it.

The batch() method can be added as an extension of the native JS Array interface.

const result1 = batch(
  [1,2,3,4,5,6,7,8,9],
  2
)
// returns [
//  [1,2],
//  [3,4],
//  [5,6],
//  [7,8],
//  [9]
// ]

const result2 = [1,2,3,4,5,6,7,8,9].dedupe(2)
// returns [
//  [1,2],
//  [3,4],
//  [5,6],
//  [7,8],
//  [9]
// ]

Installation

npm install --save array-batcher

Documentation

Extending Array with batch()

To add the batch() method to JS's base Array interface, you can import the extension by including:

import "array-batcher/extend-array-with-batch"

into your codebase at some point before attempting to call [].batch()

// somewhere in your initialization logic
import "array-batcher/extend-array-with-batch"

// ...

// Array now has a batch() method
const result = [1,2,3].batch(2) // returns [[1,2], [3]]

Keywords

FAQs

Package last updated on 20 Mar 2024

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