Coder Social home page Coder Social logo

named-array's Introduction

named-array

Access array elements via names, within ES5 standard.

Works in all browsers and Node.js versions.

Installing

$ npm install named-array

To rebuild src/named-array.min.js, install the dependencies and then run npm run build.

Usage

  • In a web browser
<script src = "./named-array.min.js"></script>

<script>
    var obj = {first: 1, second: 2};
    var a = obj.toNamedArray();
    // obj.first = a[0];
    // obj.second = a[1];
</script>
  • In Node.js
require('named-array');

var a = [1, 2, 3];
a.addProperties(['first', 'second']);
// a[0] = a.first
// a[1] = a.second

API

The API safely extends Array + Object prototypes with 2 new methods, as documented below.

Array.prototype.addProperties(props, [options]) => Array

Safely adds specified properties to the array object. And if you try adding a property that already exists on the array object, an error will be thrown. For example, you cannot add properties like length, map, etc, see Array API for existing methods and properties.

Parameters

  • props - array of strings, with names for the properties
  • [options] = {[configurable=true], [enumerable=false], [writable=false]}, with the same meaning and defaults as for Object.defineProperty.

Object.prototype.toNamedArray([options]) => Array

Converts an object into a named array.

Parameters

  • [options] = {[configurable=true], [enumerable=false], [writable=false]}, with the same meaning and defaults as for Object.defineProperty.

named-array's People

Contributors

vitaly-t avatar

Watchers

James Cloos avatar  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.