Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create objects/arrays in JavaScript?

I am fairly new to JavaScript and I want to create objects and arrays using JavaScript.

What I'm basically after is a way of creating an object that will store the following information, i.e.:

[index, name] such as [0,"NameA"][1,"NameB"] etc.

I am unsure how to create an object/array to store this type of info with jQuery.

Secondly, I then need a means of passing this object/array as a parameter into another function then be able to loop through this object/array and print out it's contents.

Again, I am unsure how to do this part as well.

like image 744
tonyf Avatar asked Dec 05 '22 06:12

tonyf


1 Answers

If you're interested in saving some data in an array, and pluck it out together with its index, why not do it the simplest way possible?

var arr = ["Alpha", "Bravo", "Charlie", "Dog", "Easy"];

for (i = 0; i<5; i++) {
    alert("Element with index " + i + " is " + arr[i] + ".");
}
like image 111
Tomas Aschan Avatar answered Dec 24 '22 14:12

Tomas Aschan