288

I know that we can easily use ng-repeat for json objects or arrays like:

<div ng-repeat="user in users"></div>

but how can we use the ng-repeat for dictionaries, for example:

var users = null;
users["182982"] = "{...json-object...}";
users["198784"] = "{...json-object...}";
users["119827"] = "{...json-object...}";

I want to use that with users dictionary:

<div ng-repeat="user in users"></div>

Is it possible?. If yes, how can I do it in AngularJs?

Example for my question: In C# we define dictionaries like:

Dictionary<key,value> dict = new Dictionary<key,value>();

//and then we can search for values, without knowing the keys
foreach(var val in dict.Values)
{
}

Is there a build-in function that returns the values from a dictionary like in c#?

Ricky Dam
  • 1,833
  • 4
  • 23
  • 42
Vural
  • 8,666
  • 11
  • 40
  • 57
  • 1
    What is the difference between a dictionary and a JSON object? I believe there is none in javascript! – markmarijnissen Jan 06 '14 at 08:28
  • 8
    @markmarijnissen: [A JSON object has more stringent syntax rules than a JavaScript object](http://json.org/). And, as we’re already in Pedant’s Corner, there’s no such thing as a “Dictionary” in JavaScript. We just call them objects. – Paul D. Waite Mar 28 '14 at 17:27

4 Answers4

564

You can use

<li ng-repeat="(name, age) in items">{{name}}: {{age}}</li>

See ngRepeat documentation. Example: http://jsfiddle.net/WRtqV/1/

DLeh
  • 23,806
  • 16
  • 84
  • 128
Artem Andreev
  • 19,942
  • 5
  • 43
  • 42
  • 53
    This is not what I asked but exacly what I wanted. Thank you Artem Andreev – Vural Aug 16 '12 at 18:15
  • 1
    How can we use filter in this ng-repeat. Its not working in my case when i am using a search filter. Say
  • {{name}}: {{age}}
  • ... – Shekhar Apr 04 '13 at 14:32
  • 1
    @Shekhar Filter "filter" works only with arrays. You can try to create feature request. – Artem Andreev Apr 06 '13 at 07:14
  • 2
    Good to see some commonality with python :) – Rusty Rob Apr 18 '13 at 03:43
  • The problem I'm running into is editing age, for example. I find that doing an ng-repeat this way & then binding name, age to input elements makes them non-editable. See [this question](http://stackoverflow.com/questions/16495749/angularjs-ng-repeat-with-key-value-updating-object) – Hitesh Aidasani Jul 09 '13 at 19:57
  • 4
    You can get filter feature even on dictionaries(objects) by using the ng-if statement.. like for example:
  • {{obj.name}} {{obj.surname}}
  • ... where items is a set of persons indexed by some key. – giowild Sep 17 '14 at 15:57
  • @giowild -- **Thanks!** I'm not sure why angular doesn't natively allow a `filter` (or `orderBy`) on object `ng-repeat`. But the `ng-if` worked great! I wish there was a way to do `orderBy` as well in the view. I get that properties are alphabetical and it orders by the property name, but it would be nice to have the option to order by something else as well. – smw Sep 19 '14 at 23:41
  • to clarify the bracketed parameters are the key and value (in that order): `
    ...
    `
    – ErichBSchulz Feb 12 '15 at 11:34
  • One problem of this approach is that (at least in Angular 1.2) it sorts key-value pairs by key. So it won't display the items in the order they are in e.g. object literal. For some (most?) use cases it's actually a useful feature though. – Anton Mochalin Mar 11 '15 at 16:28
  • Use [toArrayFilter](http://ngmodules.org/modules/angular-toArrayFilter) to enable orderBy on objects. – John Rix Apr 14 '16 at 15:42