Accessing an array's object's property, how?
What is the best way to create an array of objects in Javascript? - Stack Overflow
Objects vs Arrays
[Javascript] The difference between Arrays and Objects?
The point of arrays is that you use them to store multiple items of the same kind. Like, for some computation you have one million data points from a sensor. They go in an array. You then iterate over the array and do some computation on each element, uniformly the same computation for each one.
In an object, the whole object represents one whole thing, which has some number of specific properties that are represented by the members of the object. You use that for something like a person, who has a first name, a last name, a phone number, an email address, and a street address. You don't normally iterate over those different pieces of information to do the same computation on all of them. You might, on the other hand, store a number of persons in an array of person objects, and iterate over them for instance to search for all persons who live on a particular street.
More on reddit.comVideos
Creating an array is as simple as this:
var cups = [];
You can create a populated array like this:
var cups = [
{
color:'Blue'
},
{
color:'Green'
}
];
You can add more items to the array like this:
cups.push({
color:"Red"
});
MDN array documentation
The array should be like this...
var cup = [];
After we putting properties to the array, it will be like this
[
{
"color": "blue",
"size": "large",
"type": "mug"
}
]
And you can put properties like this..
var cup = [];
cup.push({
color : 'blue',
size : 'large',
type : 'mug'
})
console.log(cup);
Hello! Just curious…do you guys like manipulating objects or arrays more? Pros and cons of both?
Personally I find dot notation with objects much much easier then iteration but I am a novice and I’m curious to know if my logic is flawed.
Thanks!
Hey - I'm working through Code Academy trying to learn Javascript so I can code within Unity.
I've just been introduced to Objects but I don't understand how they differ to arrays... Both hold multiple sets of information and it seems like the only difference is you're able to index Objects with a named category instead of trying to remember that array[1] is this and array[2] is that.
Can someone please shed more light on this for me? Thanks so much!
The point of arrays is that you use them to store multiple items of the same kind. Like, for some computation you have one million data points from a sensor. They go in an array. You then iterate over the array and do some computation on each element, uniformly the same computation for each one.
In an object, the whole object represents one whole thing, which has some number of specific properties that are represented by the members of the object. You use that for something like a person, who has a first name, a last name, a phone number, an email address, and a street address. You don't normally iterate over those different pieces of information to do the same computation on all of them. You might, on the other hand, store a number of persons in an array of person objects, and iterate over them for instance to search for all persons who live on a particular street.
In JavaScript, arrays are objects, where the position of a value in the array (its numerical index) is its key. Seriously, try my_array isInstanceOf Object. They also have a few special extra methods and properties that normal objects don't have, like push and pop, length, indexOf, etc.
Arrays are good for storing sequences of things where the order is important. Let's say you want to represent a line (or queue) of people waiting to buy tickets for a movie. Then you can use an array to store the people's names, and the position in the array will represent their position in line (starting at zero). Like:
var ticket_line = ['alice', 'bob', 'carol', 'dave'];
// A new person shows up in line
ticket_line.push('eve');
// The first person in line gets her ticket
ticket_line.shift(); // 'alice' gets her ticket
ticket_line.length; // Four people in line
You could do this with an object, but... well, don't. Use the right tool for the job. And similarly, you could use non-integer keys in an array (e.g. my_array['four'] = 4; is perfectly valid in JavaScript), but again, don't do this.
More generally though, objects (or associative arrays, dictionaries, etc. as they may be known in other languages) and arrays (or lists) are NOT necessarily going to be so similar in other languages. But they are still going to serve generally the same purpose. Arrays for storing an ordered sequence of values, and objects for storing a key which maps to a value.