npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

myao

v1.3.3

Published

Manage Your Array of Objects

Downloads

10

Readme

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