array-add-item

Add item to array

Usage no npm install needed!

<script type="module">
  import arrayAddItem from 'https://cdn.skypack.dev/array-add-item';
</script>

README

add item in array

XO code style

Just a simple function to add an item into an Array. Works well in both, the browser & node.

install

npm i -S array-add-item

and use it

import addItemInArray from 'array-add-item'
// OR
const addItemInArray = require('array-add-item')

const abcde = addItemInArray(['a', 'c', 'd', 'e'], 'b', 1)
console.log(abcde) // ['a', 'b', 'c', 'd', 'e']

behavior

  • addItemInArray is a pure function, it does not mutate the provided array.
  • item is added at the end if no position is provided or it is out of the range of the array (grater than arr.length or less than 0).
  • returns the array if the position provided is not a number or is a float number.

FAQ