Giter Site home page Giter Site logo

slice-object's Introduction

slice-object v1.2.0

Fastest way to slice an object into a new array.

Almost identical to [].slice, yet faster in all cases. The only difference: holes are filled in, which means sparse arrays are never returned. You can even use this function to convert a sparse array into a packed array!

slice(obj: Object, start: number = 0, end: number = obj.length) : Array

Usage

const slice = require('slice-object');

// Works with any object that has a `length` property.
const obj = {
  0: 0,
  1: 1,
  2: 2,
  length: 3,
};
slice(obj);       // => [0, 1, 2]
slice(obj, 1);    // => [1, 2]
slice(obj, 1, 2); // => [1]

// Perfect for `Arguments` objects.
function args() { return arguments }
slice(args(1, 2, 3, 4), 2); // => [3, 4]

// Slicing arrays is faster, too!
slice([1, 2, 3], 1); // => [2, 3]

// When the start index is >= the length, an empty array is returned.
slice(args(1, 2), 2); // => []

// When the start index is >= the end index, an empty array is returned.
slice(args(1, 2), 1, 1); // => []

// Negative indexes are supported.
slice(args(1, 2, 3), -2, -1); // => [2]

// Create a packed array from a sparse array.
const arr = [];
arr[1] = 1;
arr.hasOwnProperty(0);        // => false
slice(arr).hasOwnProperty(0); // => true

slice-object's People

Contributors

aleclarson avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.