Code Monkey home page Code Monkey logo

pyjsarray's Introduction

PyJsArray

Build: adamauckland

View all the tests

View on GitHub

Do you love Python list slicing?

Want to do this..

items = ["apple", "ball", "cat"]

items[-1:] # returns ["cat]

items[-1] # returns "cat"

items[:1] # returns ["apple"]

Then this module is for you!

Usage

Create an array:

const array = [
	"apple",
	"ball",
	"cat",
	"dog"
];

Create a PyJsArray object, passing your array in:

const newArray = new PyJsArray(array);

Accessing from the end backwards:

newArray[-1] // returns "dog"

PyJsArray also accepts the start:end format, which must be quoted or JS will think it's an object

newArray["-2:"] // returns ["cat", "dog"]

Stepped slicing

Stepped slicing. A third "step" parameter can be passed in to return every 2nd, 3rd or nth item. It also works in reverse.

const array = [
	"apple",
	"ball",
	"cat",
	"dog",
	"elephant",
	"fish"
];

newArray["1:5:2"] // returns ["ball", "dog" ]

When reversing, start index must be higher than end index.

newArray["5:1:-2"]
  • END INDEXES ARE NON-INCLUSIVE. *

In the above example, we do not get "fish" returned because "fish" is at index[5] and as 5 is our end index (non-inclusive), it gets ommitted.

That's how it works in Python.

Javascript uses : so when you want to use : QUOTE IT! sorry.

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.