The mJS is a restricted JavaScript engine designed for microcontrollers with limited resources. The array type does not support standard Array prototype to limit the library footprint.
This library adds some of the JavaScript Array prototype utils. It modifies the prototype of an array instance. As different instances of array object does not share the same prototype object, each instance needs to be initialized separately. An array can be initialized by the Array.create() method. Arrays returned from utility functions are already initialized.
Import library in mos.yml
:
libs:
- origin: https://github.com/mongoose-os-libs/mjs
- origin: https://github.com/Dietatko/mjs-array
Use in javascript code:
load("mjs_array.js");
Array.create([ 1, 2, 3 ])
.map(function(x) { return x * 2; })
.every(function(x) { return (x % 2) == 0; });
Following functions are available:
- Array.create() - initializes an array instance
- Array.from()
- Array.prototype.every()
- Array.prototype.filter()
- Array.prototype.find()
- Array.prototype.findIndex()
- Array.prototype.forEach()
- Array.prototype.map()
- Array.prototype.reduce()
- Array.prototype.some()
The Array.create() method sets the prototype of an array instance object.
Array.create([array])
array
- The source array to be initialized.
The initialized instance of the array.
The function return the initialized instance of the array. If no source array was supplied, an empty array is used. If non-array instance is supplied, null
is returned.
Not all functions are available to limit amount of storage and memory needed. There is no option of including only some of the functions at the moment.