0.0.1 • Published 6 years ago

object-to-associative-array v0.0.1

Weekly downloads
17
License
MIT
Repository
github
Last release
6 years ago

object-to-associative-array

Introduction

Convert and array or object to an associative array. Something like PHP array.

Installation

npm install object-to-associative-array

Requirements

NODE v0.8.0 or higher

I/O

Input

  • Input should be an object / array of objects.
  • Object values can be primitive data, object or array.

Output

  • Returns an associative array.
  • Each item of the array is an object with only one key:value pair.
  • Deep conversion is done by default.
  • Pass false for shallow conversion.

Configure Options

  let configureOptions = {
    deep: true, // whether or not to do deep conversion
    discard: [] // discard this values in the result array
  };
  
  let arr = toAssociativeArray({}, configureOptions);

Examples

Simple Conversion
const toAssociativeArray = require('object-to-associative-array');

let obj = {
  a: 1,
  b: 2,
  c: {
    d: 4,
    e: 5
  }
};

let arr = toAssociativeArray(obj);
// [{"a":1},{"b":2},{"c":[{"d":4},{"e":5}]}]
Conversion With Discard
const toAssociativeArray = require('object-to-associative-array');

let opts = {
  discard: [null, undefined]
}

let obj = {
  a: 1,
  b: null,
  c: {
    d: 4,
    e: undefined
  }
};

let arr = toAssociativeArray(obj, opts);
[{"a":1},{"c":[{"d":4}]}]
Deep Convertion

Default convertion is default.

const toAssociativeArray = require('object-to-associative-array');

let obj = { a: 1, c: { e: 'e1', 'e2', g: { i: 9, j: { k: 11, l: 12 } } } };

let arr = toAssociativeArray(obj); // [{"a":1},{"c":[{"e":{"0":"e1"},{"1":"e2"}},{"g":[{"i":9},{"j":[{"0":{"k":11},{"l":12}}]}]}]}]

##### Shallow Conversion
```javascript
const toAssociativeArray = require('./index');

let opts = {deep: false};

let obj = {
  a: 1,
  c: {
    e: ['e1', 'e2'],
    g: {
      i: 9,
      j: [
        {
          k: 11,
          l: 12
        }
      ]
    }
  }
};

let arr = toAssociativeArray(obj, opts);
// [{"a":1},{"c":{"e":["e1","e2"],"g":{"i":9,"j":[{"k":11,"l":12}]}}}]

External Dependencies

N/A

Credits

Original Author

Lisence

MIT