Showing posts with label index. Show all posts
Showing posts with label index. Show all posts

Monday, January 13, 2014

Sorted Array Push Using Lodash

Using the Array.push() method, you can push elements onto the end of a given array instance. That's handy for when you're first building an array, and assuming that you're not maintaining the sort order of the array. But what if you've already got an array, one that's populated with elements? In this case, Array.splice() is your friend — you use this to insert elements at the specified index position. If all you're doing is trying to add elements to an array while maintaining the sort order, you can use Lodash.

Thursday, January 3, 2013

Indexes and Keys

In any programming language, the developer typically has a choice between two collection types — a list or a dictionary. They may be called something similar, depending on the language — array and hash-map for example. The list uses an index to access a given item in the collection while the dictionary uses a key. A key could be a number, or some other primitive construct in the language, but they're usually strings. There is one interesting distinction between indexes and keys. Indexes are allocated by the list whereas keys have to be dreamed up by the programmer. This is both good and bad. Good because keys are strings based on some concept. Indexes, by contrast aren't so intuitive to the developer when they need to look something up later. Should we just stick with dictionaries in our code, or our we introducing some hidden issues?