15

I just found out that Arrays inherit directly from Object in javascript. I'm finding the difference between an array and an object is fairly minuscule.

How do i know when to use one over the other?

Derek Adair
  • 21,846
  • 31
  • 97
  • 134

6 Answers6

19

When you need to depend on the order of the elements in the collection, use Arrays, when order is not important, use objects. Order is not guaranteed in objects, but they provide for fast key-value pair lookups.

Alex
  • 64,178
  • 48
  • 151
  • 180
11

I'd use an Array [] when I'm dealing with a list of objects the same type* (like a list of strings) that I want the order of and an Object {} when I'm dealing with something that I want properties of (and the properties are of different types, usually).

For example:

var numberNames = ["one","two","three","four"];

var computer = {
     motherBoard : "mother", 
     psu : psu_object(),
     ram : { 
             ram1 : GB,
             ram2 : TwoGB
     }
};

* This is just my preference. Arrays can contain elements of multiple different types.

Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46
8

They are just different data structures, they serve different purposes. Array extends Object and provides properties like length and methods like push() and pop().

Think of an object as a hash table and an array as a list.

E.g. you can use arrays as queue or as a stack which would not be possible with objects.

On the other side if you want to store data and want to access a specific datum directly, you would use an object.

In the end it boils down to the question which data structure is the right one for the job. Maybe neither of them is and you would need a tree instead (which can be implemented via objects).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
4

Objects are a good candidate if you have a unique key.

Example Array vs. Object:

var item = { id: '123', prop1: 456 };

var obj = {}, arr = [];

// add to object
obj[item.id] = item;

// add to array
arr.push(item);

// get from object
var got = obj[item.id];

// get from array
var got;
for(var i = 0; i < arr.length; i++)
    if(arr[i].id == item.id){
        got = arr[i];
        break;
    }

As you can see finding an object is much more expensive with an array (loop). Using an object you will not have access to all the array helper methods (sort/push/etc) but you can still iterate an objects' properties using a for...in loop.

Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
3

Objects keys are strings; array keys are integers. JavaScript objects are maps (String -> anything) and arrays are lists (ordered collections ofanything).

Does that help?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Just for the record: JavaScript basically treats its arrays like objects. That means those "integer" keys in arrays are actually strings. (This is especially important to note if e.g. you have an array or function key; it will convert those to strings...) Also therefore you don't really get the performance boost that you would in other languages from using arrays over objects. – Andrew Apr 08 '20 at 21:07
  • 1
    Anyways this is the real answer. Use objects for when you need key->value, use arrays for when you just need value or index->value. – Andrew Apr 08 '20 at 21:08
0

You often use arrays when you have a series of related items that you want ordered and indexed.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356