4

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.

1
  • You'd better change the tag for your question, since it hardly related to jquery. It's about javascript syntax. Commented May 23, 2009 at 12:03

4 Answers 4

10

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] + ".");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Lol at the deviation from the phonetic alphabet ;-)
4

You may find the following article interesting

Essentially, the (jQuery) object returned by the jQuery $() function has a bunch of properties, which include one for each element that matches the selector, with the property name being a numeric "index"

For example, given the following HTML

  <p>Hello, World!</p>
  <p>I'm feeling fine today</p>
  <p>How are you?</p>

and the selector

$('p');

the object returned will look as follows

({length:3, 0:{}, 1:{}, 2:{}})

Using the .get() command, you can access matched elements and operate on them accordingly. Following with the example

$(function () {
var p = $('p').get();
for (var prop in p)
  alert(prop + ' ' + p[prop].innerHTML);
});

or alternatively, knowing how the returned object is structured

$(function () {
var p = $('p');
for (var i=0; i< p.length; i++)
  alert(i + ' ' + p[i].innerHTML);
});

will alert

0 Hello, World!
1 I'm feeling fine today
2 How are you?

I know that I have not answered your question directly, but thought that it may be useful to provide insight into how jQuery works.

I think what you need in order to answer your question is either

  • a simple array, as demonstrated by Tomas Lycken's answer. This seems to best fit what you are asking for

    var mySimpleArray = ['a','b','c'];

  • an array of objects, with each object having an 'index' and 'name'. I'll make the assumption here that 'index' is implied to be be any number that you want to assign and does not imply an ordinal position

    var myObjectArray = [{ index: 5, name: 'a' },{ index: 22, name: 'b'},{ index: 55, name: 'c'}];

4 Comments

Thanks Russ for that info. One question, how would I go about setting up the var myObjectArray set-up, which is what I'm after - is that the same as what Martin has provided me?
Yes, Martin's answer gives you a class that has index and name properties and then demonstrates how you would add that type of object to an array.
shouldn't you account for the length: 3 at the beginning of the jquery object? thanks
@butterywombat - which part are you referring to? Did you mean explain why the jQuery object has a length property?
1

The doc page for $.each should solve your problem:

http://docs.jquery.com/Utilities/jQuery.each

From the above link:

var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };

jQuery.each(arr, function() {
  $("#" + this).text("My id is " + this + ".");
  return (this != "four"); // will stop running to skip "five"
});

jQuery.each(obj, function(i, val) {
  $("#" + i).append(document.createTextNode(" - " + val));
});

Comments

1
//class
function Foo(index, name) {
this.index = index;
this.name = name;}
//object of that class
var obj = new Foo(0, "NameA");

var myArray = new Array();
//add object to array
myArray.push(obj);    

function loopArray(someArray) {
var index; 
var name;
for (var i = 0; i < someArray.length; i++) 
 {
    index = someArray[i].index;
    name = someArray[i].name;
 }
}

6 Comments

Too heavy for that purpose, it could be simple array_name[0]="NameA".
I read the question as he wanted to be able to set the index himself.
Martin, with your response, how would I go about adding more info to the Foo object as this will be growing dynamically?
obj = new Foo(1, "NameB"); myArray.push(obj); obj = new Foo(2, "NameC"); myArray.push(obj); and so on... You can do this in a loop or something. I think this works as well myArray.push(new Foo(3,"NameD"));
or maybe i misunderstood. did u mean this? function Foo(index, name, newinfo) { this.index = index; this.name = name; this.newinfo = newinfo;} var obj = new Foo(1,"NameA","New Stuff");
|

Your Answer

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.