|
|
PiMunkey Documentation ->
Arrays vs. Objects
Collections of data in Javascript are commonly stored in either objects or arrays. With Pi Programming, both are handled by the same object. Array NotationOrdered arrays are not accessed by named attributes, but by their index within the object. You can use the "add", "insertAt", and "remove" functions to add and remove properties based on their location in the object, rather than their name. Example: var site = newMunkey(), page1 = newMunkey(title="Page1"), page2 = newMunkey(title="Page2"), index = site.add(page2); site.insertAt(index,page1); site.remove(2);
Result: The above example creates two pages and adds them to a Pi Array called "site". The second page is added first by appending it to the end of the array. The first page, in this example, is then inserted before the second page, putting the pages in the correct order. As a last step, the second page is then removed.
Object NotationNamed properties are added by their IDs, and can be accessed in the same way. They can also be added one at a time or in batches. Example: var page = newMunkey(); page.set("id","page1"); page.set(title="First Page", template="firstpage.html", group="products"); page.remove("group"); return page.get("title");
Result: The above example sets four properties, "id", "title", "template", and "group", then removes the "group" property and returns the "title" property.
|
|
Copyright ©2010, MillionMunkeys® LLC, All Rights Reserved |