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

angular-toarrayfilter

v1.0.3

Published

An angular filter to convert objects to arrays for easy filtering and sorting

Downloads

7,242

Readme

AngularJS toArray Filter

Although AngularJS 1.x supports iterating over an object (keys and values), it is not the preferred way of doing things. Moreover, filters like filter and orderBy just don't work with objects; they are designed to work with arrays.

This filter can convert your objects into stable arrays that can then be filtered and sorted using the standard AngularJS filters.

Get It

The easiest way to install is using bower:

bower install --save angular-toArrayFilter

Alternatively you can download from the GitHub project: https://github.com/petebacondarwin/angular-toArrayFilter

Load It

Load the toArrayFilter.js file into your web app after loading angular.js

<html>
  ...
  <head>
    ...
    <script src="angular.js"></script>
    <script src="bower_components/angular-toArrayFilter/toArrayFilter.js"></script>
    ...
  </head>
  ...
</html>

Use It

Make sure that your AngularJS application references the angular-toArrayFilter module:

angular.module('myApp', ['angular-toArrayFilter']);

Now if you have an object that you wish to repeat over, just slip the toArray filter in before you try sorting or filtering:

<div ng-repeat="item in itemObj | toArray | orderBy : 'someProp'">
  {{ item.$key }} - {{ item.someProp }}
</div>

How it works

The filter iterates over the object's keys and creates an array containing the value of each property. By default, the filter also attaches a new property $key to the value containing the original key that was used in the object we are iterating over to reference the property.

Not adding the $key property

If you don't want the $key property to be attached to each of the property values, you simply put an additional parameter on the toArray filter:

<div ng-repeat="item in itemObj | toArray : false | orderBy : 'someProp'">
  {{ item.someProp }} (no $key available now)
</div>

Using $key with non-objects

Non-objects such as strings and numbers cannot have a new $key property attached to them. If the object properties you are iterating over are not objects then you must either disable the $key property or the filter will replace the non-object with a new object of the form: { $key: key, $value: value }.

Caveats

There are always issues when trying to iterate over properties in JavaScript and the toArray filter has its own set of things to be aware of when using it:

  • It only works with plain Objects - don't try to filter arrays and strings with it.
  • If you don't disable it, the filter will modify each property value with a new $key property.
  • This filter is not compatible with IE8. (It uses Object.keys and Object.defineProperty which don't work well or at all in Internet Explorer 8 (IE8).

Example

Here is a fuller example of using the toArray filter on an object, to allow sorting and filtering by a date property on each property of the original object. It also demonstrates how you can easily update the original object and the array will stay in sync.

Check out the Live Demo

index.html:

<div ng-app="app">
  <div ng:controller="Main">
    <div ng:repeat="item in items | toArray | orderBy: 'date'">
      {{item.$key}}: {{item.date | date}}
      <button ng-click="remove(item)">Remove</button>
    </div>
    <button ng-click="add()">Add</button>
  </div>
</div>

app.js

angular.module('app', [])

.controller('Main', function Main($scope) {
  $scope.nextKey = 4;
  $scope.items = {
    0: {date: new Date('12/23/2013')},
    1: {date: new Date('12/23/2011')},
    2: {date: new Date('12/23/2010')},
    3: {date: new Date('12/23/2015')}
  };

  $scope.remove = function(item) {
    delete $scope.items[item.$key];
  };

  $scope.add = function() {
    $scope.items[$scope.nextKey] = { date: new Date() };
    $scope.nextKey += 1;
  };
});