Coder Social home page Coder Social logo

dantehemerson / arr2obj Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 233 KB

:tada: Converts an array to object, with custom key and value.

License: MIT License

TypeScript 98.57% JavaScript 1.43%
arrays arr2obj parser tranform typescript-library node-module package

arr2obj's Introduction

arr2obj

Build Status Licence npm

Converts an array to object, with custom key and value.

Install

npm i @dantehemerson/arr2obj

or with yarn:

yarn add @dantehemerson/arr2obj

Usage

You can import as follows:

const { arr2obj } = require('@dantehemerson/arr2obj')

The function is:

arr2obj(array, [options])

Arguments

array: The array to transform.

options: The options sould be a object with two optional keys: key and value, functions that receive the item as argument and return a value.

  • key: Function that must return the value that will be taken as the key.

  • value: Function that must return the value that will be taken as value.

for example:

{
  key: item => `pre-${item}`,
  value: item => item
}

If you use Typescript, the interface of options is:

interface Options {
  key?: (item: any) => object | number | string | boolean
  value?: (item: any) => object | number | string | boolean
}

Returns

Object tranformed.

Examples

Array of basic types:

const result = arr2obj(['a', 'b', 'c'])
// result:
// { a: 'a', b: 'b', c: 'c' }

Array of objects

const arr = [
  {
    id: 1
  },
  {
    id: 2
  },
  {
    id: 3
  }
]

const result = arr2obj(arr, {
  key: item => `pre-${item}-post`,
  value: item => Math.pow(item, 2)
})

/**
  result:
  {
    1: {
      id: 1
    },
    2: {
      id: 2
    },
    3: {
      id: 3
    }
  }
**/

Author

Dante Calderon - @dantehemerson

Licence

MIT

arr2obj's People

Contributors

dantehemerson avatar dependabot[bot] avatar renovate-bot avatar

Stargazers

 avatar

Watchers

 avatar  avatar

arr2obj's Issues

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.