• Stars
    star
    142
  • Rank 257,111 (Top 6 %)
  • Language
    JavaScript
  • Created about 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A filter to convert your objects to an array that can be filtered and sorted easily

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

You can also install via [npm][npm]

npm 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;
  };
});

More Repositories

1

dgeni-example

A really simple dgeni configuration
JavaScript
137
star
2

ng1-component-demo

A demo application for ng-conf 2016 to demonstrate a component based architecture with Angular 1.5
HTML
99
star
3

dgeni-angular

JavaScript
55
star
4

github-pr-helper

This adds sorely needed features to GitHub's PR pages.
CSS
49
star
5

questionnaire-angular

JavaScript
42
star
6

ng1-component-router-demo

JavaScript
30
star
7

AngularJS-Directives-Presentation

JavaScript
23
star
8

angular-firebase

Firebase backend integration for AngularJS
JavaScript
23
star
9

angular-exchange-rates

A simple demo of a currency converter application
JavaScript
20
star
10

super-simple-angularjs-requirejs-app

Super Simple AngularJS RequireJS App
JavaScript
17
star
11

angular-ui-foundation

JavaScript
13
star
12

foodme-old

FoodMe AngularJS Workshop Application
JavaScript
13
star
13

angular2-webpack2-ngrx-starter

TypeScript
11
star
14

foodme

JavaScript
6
star
15

commit-reader

JavaScript
5
star
16

SilverStripe-Associated-Folder-Module

A module for Silverstripe CMS that allows a Page to have an associated Folder in the file system.
PHP
5
star
17

pg-demo

HTML
4
star
18

angular-mask-directive

A masked input directive for AngularJS
JavaScript
3
star
19

angularjs-testing-presentation

Angular Testing Presentation
JavaScript
3
star
20

foodme-intro

AngularJS Introductory Workshop App
HTML
3
star
21

kanso.spine.todos

A version of the standard ToDo list application in Spine but so that it can be pushed with Kanso.
CoffeeScript
2
star
22

kanso-precompiler-base

A small library to help build Kanso precompilers
CoffeeScript
2
star
23

cf-external-dep-demo

TypeScript
1
star
24

auction

TypeScript
1
star
25

Questionnaire

An online-offline questionnaire CouchApp - built with Kanso
JavaScript
1
star
26

changez

TypeScript
1
star
27

foodme-further

JavaScript
1
star
28

localize-icu-test

TypeScript
1
star
29

upgrade-test

TypeScript
1
star
30

hybrid-viewengine-partial-library-demo

TypeScript
1
star
31

Umbraco---Optional-Boolean-Data-Type

A data type renderer for Umbraco that allows true, false and null values.
C#
1
star
32

Student-Grouping-App

JavaScript
1
star
33

kanso.spine.simplest

The simplest Spine app pushed to CouchDB by Kanso
JavaScript
1
star
34

ng2-http-playground

JavaScript
1
star
35

issue32912-i18n-no-legacy-test

TypeScript
1
star
36

qwik-app-cf

TypeScript
1
star
37

Elf

A light-footed, agile content management framework
JavaScript
1
star
38

SilverStripe-Photo-Album-Module

A module for displaying a simple photo album in a page.
PHP
1
star
39

node-q-github

A Q promise-based wrapper around the node-github API
1
star
40

Silverstripe-HTML-Purifier-Module

Cleans up html using the HTMLPurifier
PHP
1
star
41

kanso-es5shim

Kanso package to provide compatibility shims so that legacy JavaScript engines behave as closely as possible to ES5.
JavaScript
1
star