0

I am trying to create a array with multiple fields in it.

For Example:

var person1 = {firstname:"Bob", lastname:"Smith", middlename:"happy"};

The problem I have is that I have 5000 variables I want to create so it would become:

var person1 = {firstname:"Bob", lastname:"Smith", middlename:"happy"};
var person2 = {firstname:"John", lastname:"Jones", middlename:"Long"};
..
var person5000 = {firstname:"Jim", lastname:"Cook", middlename:"Shorty"};

I think it would be silly to have 5000 lines of code to declare the variables. So I want to be able to declare the variables on page load and then later assign the values to each.

I was trying to do this using the following code but I am guessing I am doing something wrong. (I am loading some dummy data into the variables for testing)

<!DOCTYPE html>
<html>
<body>
<script>
 var person = new Array (firstName:"", lastName:"", middleName:"");
 for (var i = 0; i < 5000; ++i) {
 person[i] = {firstName:"First"+i, lastName:"Last"+i, middlename:"Middle"+i};
 }
alert(person1["firstName"]); // should alert First1
alert(person6["lastname"]); // should alert Last6
</script>
</body>
</html>

I was hoping to later in my code set the value using: (I am pretty sure this code should work, but can't test it since I can't declare the variables correctly)

person1[firstname] = "Terry"; // should replace First1 with Terry

And then to receive a value using:

alert(person1[firstname]); // should alert Terry since it was changed

Anyone know what is wrong with my code since it's not returning the value ? I am guessing I am declaring the variables wrong? If so how should I declare them ?

asked Nov 10, 2014 at 0:48
1
  • array != object Commented Nov 10, 2014 at 0:51

2 Answers 2

2

You appear to be confused about the difference between arrays and objects in Javascript. Arrays have numeric indexes, objects have named properties. So the initialization

new Array(firstName:"", lastName:"", middleName:"");

makes no sense. Not to mention, it's not valid Javascript syntax; property: value pairs can only be used in object literals, not in argument lists. If you use new Array(...), the argument should either be a single number, which is the size of the array to allocate, or a list of initial array element (with no property: prefixes. But the preferred way to create a new array is simply with the [] literal for an empty array; it will grow as necessary when you assign to it.

When you create an array, you don't get separate variables for each element. You access them using array[n] notation.

// Create an empty array
var person = [];
// Fill up the array
for (var i = 0; i < 5000; ++i) {
 person[i] = {firstName:"First"+i, lastName:"Last"+i, middlename:"Middle"+i};
}
// Access elements
alert(person[1].firstName);
alert(person[6].middleName);
// Change elements
person[1].firstName = "Terry";
answered Nov 10, 2014 at 0:53
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for explaining that to me.. Makes sense now.
I was following the example from: w3schools.com/js/js_arrays.asp and must of got confused when following there examples.
Although I generally recommend against w3schools.com (they're prolific, but the quality is poor), that page does have a section explaining the difference between objects and arrays, and explaining when to use one versus the other.
1

I believe this should work as you intended:

var person = new Array();
for (var i = 0; i < 5000; ++i) {
 person[i] = {firstName:"First"+i, lastName:"Last"+i, middleName:"Middle"+i};
}
alert(person[1]["firstName"]);
alert(person[6]["lastName"]);

As pointed out by others, the person array is filled with objects, not arrays. You can use either property or associative array syntax with them.

answered Nov 10, 2014 at 0:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.