Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top