Home

Javascript Difference Between Arrays and Objects

Damnably confusing for novices like me. Is it an array or an object? Let's make it simple

And array is a list, an object is a named list. 

So an array is a comma separated list between square brackets, like a shopping list

["Cheese", "Onions", "Bread", "Milk", "Peanut Butter"]

An object just gives each item a name to define it and uses curly brackets, here's some car data

{"Make":"Ford", "Model":"Fiesta", "CC":"1300", "Color":"Red"}

And THAT IS IT!! Oh you can make it much much MUCH more complex. 

You could have a list of objects, an array of objects

var MyArray = [
                {"Make":"Ford", "Model":"Fiesta", "CC":"1300", "Color":"Red"},
                {"Make":"Vauxhall", "Model":"Astra", "CC":"1800", "Color":"Green"},
                {"Make":"Suzuki", "Model":"Jimny", "CC":"1200", "Color":"Blue"},
                {"Make":"Ford", "Model":"Fiesta", "CC":"1600", "Color":"Brown"}
              ] 

How do you refer to Suzuki Jimny's 1200 engine?

MyArray[2].CC

That's MyArray 3rd item (we count from 0 hence [2]) and the CC bit of the third item. Then you can have arrays in arrays that have object with arrays containing objects....

MyArray[2].CCOptions[4].Over1.6.Petrol[4]... 

When you're buried this deep I'd have a rethink.

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog