0

I'm a student and new to Angular, so I apologize if this is obvious.

I see Angular used frequently for displaying lots of data. From what I've seen, it works fantastically when your data is stored as an array, but only puts up a fight when you try to use it with an associative array (Possible, but requires work-arounds).

Scenario:

I've got an array of kittens:

var kittens = [
  {
    id: "id0",
    breed: "persian",
    name: "Mr. Cuddles"
  },
  {
      id: "id1",
    breed: "siamese",
    name: "Puddin' Face"
  },
  {
    id: "id2",
    breed: "ragamuffin",
    name: "Rumblemuffins"
  }
];

And there are a lot of kittens. I have an angular factory kittenService that retrieves this data from an API and maintains this array. I need the ability to look up these kittens by id and filter by breed. I turned this into an associative array with id as the key, but then I run into issues with $filter, etc. There isn't as much documentation for associative arrays, and it all seems just easier to implement using regular arrays.

tl;dr

If AngularJS is used to act on data, Why are associative arrays not as common in AngularJS applications? Should I think differently about data storage when using Angular?

13
  • wouldn't your api / database be quicker at doing the query and delivering the already filtered data to the client? Commented May 9, 2014 at 16:27
  • @adrichman Unfortunately, this filtering needs to be done client-side. Filtering is used only a little bit for views, and mostly for front-end javascript behind the scenes. Commented May 9, 2014 at 16:31
  • 2
    They're called "objects"; JavaScript doesn't technically have "associative arrays", although they act pretty much the same in this case. Commented May 9, 2014 at 16:53
  • @Blazemonger I know they're at least similar constructs, but you're saying that the notation kittens["id0"] (if I wrote kittens as an associative array) would be equivalent to kittens.id0 (if I were to rewrite kittens as an object)? Commented May 9, 2014 at 16:57
  • 2
    There are no "associative arrays", just objects. And yes, those syntaxes are equivalent in JS. Try it yourself. Commented May 9, 2014 at 16:58

1 Answer 1

1

You cannot refer to (what you are calling an array but in reality are objects aka hashes) associative arrays by index and you cannot do things like check the length property. My guess is it is much more efficient to work on actual arrays than to pretend that object hashes are arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.