arrobj

Array and Object handling

Usage no npm install needed!

<script type="module">
  import arrobj from 'https://cdn.skypack.dev/arrobj';
</script>

README

arrobj


Easily modify Arrays and Objects in javascript

Installation

  • via npm:
npm install arrobj
  • via bower
bower install arrobj

Usage

  • In node:
// vanilla javascript
const arrobj = require("arrobj");
// es6 syntax
import * as arrobj from "arrobj";
  • In browser:
<script src="path/to/arrobj.js"></script>

Convert Array to Object

Below code:

var values = ['bliss', 21, ['soccer', 'basketball']];
var key = ['name', 'age', 'sports'];
console.log(arrobj.toObj(values, key));

Will create object:

{
  name: 'bliss',
  age: 21,
  sports: ['soccer', 'basketball']
}

If the key array is shorter than the values array, the index of the values in the array will used instead. The produced array for the above use case will be:

{
  0: 'bliss',
  1: 21,
  2: ['soccer', 'basketball']
}

Convert Object to Array

To convert an object to an array, the below case:

var obj = {
  name: 'bliss',
  age: 21,
  sports: ['soccer', 'basketball']
};

console.log(arr.toArr(obj));

Will produce array:

['bliss', 21, ['soccer', 'basketball']]

Other Methods

Arrays

  • isFlat(array): Checks if an array has a nested array.

  • isEqual(array1, array2): Compares two arrays by length.

  • isSame(array1, array2): Checks if the values in the array are the same, works for deep level comparison.

  • Arr(array): Checks if an argument is an array.

Objects

  • isObj(obj): Checks if argument is an object, returns true or false.

Contributing

  • Fork this repo.
  • clone and do you.
  • send a pull request.