Implements array-like methods for Object with support for dot notation keys
npm install dot-object-array







forEach for arrays, eh ?)javascript
// Import data at creation
var doa = new ObjectArray({
item1: 3,
item2: 12,
item3: 5
});// Add data
doa.push('item4', 4); // Single item or dataset
doa.import({
item5: 5,
item6: 6
});
// Add data with dotted notations
doa.push('dat.long.darn.key','isntIt?'); // Will automatically create each keys
// Define default values
// Will keep value if existent or create keys with value if not
doa.define('options.section1.item14', true);
//Iterate on keys at root level or in sub dataset
doa.forEach(function(value, key, index) {
[...]
});
doa.forEach(function(value, key, index) {
[...]
}, 'dat.path.to.data');
// Sub dataset import
doa.import({
subitem1: 1,
subitem2: 'astring',
subitem3: {obj: really}
}, 'dat.long.and.far.away.key');
// sub dataset access
doa.dataset('dat.long.and.far.away.key');
// or
doa.pull('dat.long.and.far.away.key');
// And many more !
`
Installation
$3
The ObjectArray class is provided as a UMD module.
`
npm install dot-object-array
`
or
`
yarn add dot-object-array
`
Then simply require/import it :
`javascript
import ObjectArray from 'dot-object-array';
const ObjectArray = require('dot-object-array').default;
`
ObjectArray have been coded with ECMA6 class standard.$3
DOA is available as CDN external link or can easily be installed locally.
#### Bundle
`html
`
#### JsDelivr
`html
`
#### Unpkg
`html
`
#### Local install
For browser install, you can simply fetch file dist/objectarray.min.js in this repo or clone it and load it :
`html
`
An ObjectArray constructor will be added to global (window) scope.Configuring ObjectArray behaviour when a required key doesn't exist
In version 1.x, a non-existent key yields to an undefined returned value.In version 2.x, a non-existent key data request raises an exception except for some methods that can leverage a throwable trigger.
Since version 3.x, the behaviour can be configure per method call or override globally. Each data request have a predefined behaviour
given its goal. For instance, the empty method will throw an exception by default and a dataset call will return undefined by default.
Please check API reference for details.
Playground
If you want to go further and try a bit, you can go to the playground.Documentation
A full documentation (manual and API reference) set is available :
- Manual
- API referenceJSON support
You can easily use ObjectArrays to manipulate JSON data. Just rely on JSON native object to import your JSON structure, manipulate it with ObjectArray ease and get it back at the end :wink:
`javascript
var jstring = '{"dat": {"long": {"path": "foo", "dream": "baz"}}}';
var doa = new ObjectArray(JSON.parse(jstring));// Let's say we want to move all dat.long stuff to a short thing
doa.push('short', doa.dataset('dat.long')).remove('dat');
console.log(JSON.stringify(doa.data)); // outputs {"short":{"path":"foo","dream":"baz"}}
``