Socket
Book a DemoInstallSign in
Socket

myao

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

myao

Manage Your Array of Objects

1.3.3
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

myao =^_^=

Manage Your Array of Objects

Latest Stable Version License NPM Downloads

Getting started:

npm install myao

//or

npm install --save myao
var myao = require('myao'),
    arrayofobj = [
    {name: 'Johnny', team: 'Black', age:28, id: '001'},
    {name: 'Simon', team: 'Red', age:32, id: '002'},
    {name: 'Leonardo', team: 'Blue', age: 18, id: '003'},
    {name: 'Don', team: 'White', age:40, id: '004'}
    ];

var myaoObj = myao.create(arrayofobj);

getAll

myaoObj.getAll()

return - array

myaoObj.getAll();

/* return
[
{ name: 'Johnny', team: 'Black', age:28, id: '001'},
{ name: 'Simon', team: 'Red', age:32, id: '002'},
{ name: 'Leonardo', team: 'Blue', age: 18, id: '003'},
{ name: 'Don', team: 'White', age:40, id: '004'}
]
*/

add

myaoObj.add(toAdd, toTheBeginning)

toAdd - single object or array of objects

toTheBeginning >= 1.3.2 - boolean - true for 'add' to the beginning of data;

return - this

myaoObj.add({name: 'Bill', team: 'Blue', age:26, id: '005'});
myaoObj.add([{name: 'Bill', team: 'Black', age:23, id: '006'}, {name: 'Sue', team: 'White', age:20, id: '007'}]);

/* myaoObj.getAll() return:
[ 
{ name: 'Johnny', team: 'Black', age:28, id: '001'},
{ name: 'Simon', team: 'Red', age:32, id: '002'},
{ name: 'Leonardo', team: 'Blue', age: 18, id: '003'},
{ name: 'Don', team: 'White', age:40, id: '004'},
{ name: 'Bill', team: 'Blue', age: 26, id: '005' },
{ name: 'Bill', team: 'Black', age: 23, id: '006' },
{ name: 'Sue', team: 'White', age: 20, id: '007' }
]
*/

myaoObj.add({name: 'Peggy', team: 'Pink', age:21, id: '008'}, true);

/* myaoObj.getAll() return:
[
{name: 'Peggy', team: 'Pink', age:21, id: '008'},
{ name: 'Johnny', team: 'Black', age:28, id: '001'},
{ name: 'Simon', team: 'Red', age:32, id: '002'},
{ name: 'Leonardo', team: 'Blue', age: 18, id: '003'},
{ name: 'Don', team: 'White', age:40, id: '004'},
{ name: 'Bill', team: 'Blue', age: 26, id: '005' },
{ name: 'Bill', team: 'Black', age: 23, id: '006' },
{ name: 'Sue', team: 'White', age: 20, id: '007' }
]
*/

get

myaoObj.get(key, id)

key - string - object key

id - unique value

return - first matching object or null

var blackbill = myaoObj.get('id', '006' )

blackbill.team //'Black'

//!!BUT!!

var blackbill = myaoObj.get('name', 'Bill' );

blackbill.team //'Blue'

!! This method return object reference so:

var blackbill = myaoObj.get('id', '006' );
blackbill.hobby = 'country music';

/* myaoObj.getAll() return:
[ 
{ name: 'Johnny', team: 'Black', age:28, id: '001'},
{ name: 'Simon', team: 'Red', age:32, id: '002'},
{ name: 'Leonardo', team: 'Blue', age: 18, id: '003'},
{ name: 'Don', team: 'White', age:40, id: '004'},
{ name: 'Bill', team: 'Blue', age: 26, id: '005' },
{ name: 'Bill', team: 'Black', age: 23, id: '006', hobby: 'country music' },
{ name: 'Sue', team: 'White', age: 20, id: '007' }
]
*/

set >= 1.3.0

myaoObj.set(key, id, obj)

key - string - object key

id - unique value

obj - object with propertys to assign

return - this

var bluebill = myaoObj.get('id', '005' );

myaoObj.set('id', '005', {job: 'Programmer'});

console.log(bluebill)
//{ name: 'Bill', team: 'Blue', age: 26, id: '005', job: 'Programmer' }

remove

myaoObj.remove(key, id)

key - string - object key

id - unique value

return - this

!! It removes FIRST matching object - to remove a group of objects use filter method

myaoObj.remove('id', '006');

/* myaoObj.getAll() return
[ 
{ name: 'Johnny', team: 'Black', age: 28, id: '001' },
{ name: 'Simon', team: 'Red', age: 32, id: '002' },
{ name: 'Leonardo', team: 'Blue', age: 18, id: '003' },
{ name: 'Don', team: 'White', age: 40, id: '004' },
{ name: 'Bill', team: 'Blue', age: 26, id: '005' },
{ name: 'Sue', team: 'White', age: 20, id: '007' } ]
*/

replace >= 1.3.0

myaoObj.replace(key, id, obj)

key - string - object key

id - unique value

obj - object with propertys to assign

return - this

var bluebill = myaoObj.get('id', '005' );

myaoObj.replace('id', '005', {name: 'Anonymous'});

console.log(bluebill);
//{ name: 'Anonymous'}

/* myaoObj.getAll() return
[ 
{ name: 'Johnny', team: 'Black', age: 28, id: '001' },
{ name: 'Simon', team: 'Red', age: 32, id: '002' },
{ name: 'Leonardo', team: 'Blue', age: 18, id: '003' },
{ name: 'Don', team: 'White', age: 40, id: '004' },
{ name: 'Anonymous' },
{ name: 'Sue', team: 'White', age: 20, id: '007' } ]
*/

overwrite >= 1.2.0

myaoObj.overwrite(newData);

newData - array

return - this

myaoObj.overwrite([{name: "Peter", team:"Green"}]);

/* myaoObj.getAll() return
[{name: "Peter", team:"Green"}]
*/

filter

myaoObj.filter(key, val);

key - string - object key ; 'use '-' for revers filter

val - single filter value or array of values

return - new myao object with filtered data

var bluered = myaoObj.filter('team', ['Blue', 'Red']);

var notblue = myaoObj.filter('-team', 'Blue');

/* bluered.getAll() return
[ { name: 'Simon', team: 'Red', age: 32, id: '002' },
  { name: 'Leonardo', team: 'Blue', age: 18, id: '003' },
  { name: 'Bill', team: 'Blue', age: 26, id: '005' } ]
*/

/* notblue.getAll() return
[ { name: 'Johnny', team: 'Black', age: 28, id: '001' },
  { name: 'Simon', team: 'Red', age: 32, id: '002' },
  { name: 'Don', team: 'White', age: 40, id: '004' },
  { name: 'Sue', team: 'White', age: 20, id: '007' } ]
*/

sort

myaoObj.sort(key);

key - string - object key ; 'use '-' for revers sort

return - this

myaoObj.sort('age');

/* myaoObj.getAll() return
[ { name: 'Leonardo', team: 'Blue', age: 18, id: '003' },
  { name: 'Sue', team: 'White', age: 20, id: '007' },
  { name: 'Bill', team: 'Blue', age: 26, id: '005' },
  { name: 'Johnny', team: 'Black', age: 28, id: '001' },
  { name: 'Simon', team: 'Red', age: 32, id: '002' },
  { name: 'Don', team: 'White', age: 40, id: '004' } ]
*/

//!!BUT!!

myaoObj.sort('-age');

/*myaoObj.getAll() return
[ { name: 'Don', team: 'White', age: 40, id: '004' },
  { name: 'Simon', team: 'Red', age: 32, id: '002' },
  { name: 'Johnny', team: 'Black', age: 28, id: '001' },
  { name: 'Bill', team: 'Blue', age: 26, id: '005' },
  { name: 'Sue', team: 'White', age: 20, id: '007' },
  { name: 'Leonardo', team: 'Blue', age: 18, id: '003' } ]
  */

each >= 1.1.0

myaoObj.each(callback)

callback - function with two parameters; first it is object in array, and second is index

myaoObj.each (function (elem, index) {
    elem.nick = elem.name.substr(0,2) + index; 
});

/* myObj.getAll() return
[ { name: 'Johnny', team: 'Black', age: 28, id: '001', nick: 'Jo0' },
  { name: 'Simon', team: 'Red', age: 32, id: '002', nick: 'Si1' },
  { name: 'Leonardo', team: 'Blue', age: 18, id: '003', nick: 'Le2' },
  { name: 'Don', team: 'White', age: 40, id: '004', nick: 'Do3' },
  { name: 'Bill', team: 'Blue', age: 26, id: '005', nick: 'Bi4' },
  { name: 'Sue', team: 'White', age: 20, id: '007', nick: 'Su5' } ]
*/

getValues

myaoObj.getValues(key)

key - string - object key ; 'use '-' for revers sort

return - array with values

var names = myaoObj.getValues('name');

console.log(names); //[ 'Johnny', 'Simon', 'Leonardo', 'Don', 'Bill', 'Sue' ]

getLength

myaoObj.getLength()

return - number - data length

var myaolength = myaoObj.getLength();

console.log(myaolength); // 6

Keywords

array

FAQs

Package last updated on 24 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.