最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to create objectsarrays in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 24, 2019 at 16:56 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked May 23, 2009 at 11:10 tonyftonyf 35.6k53 gold badges165 silver badges260 bronze badges 1
  • You'd better change the tag for your question, since it hardly related to jquery. It's about javascript syntax. – Artem Barger Commented May 23, 2009 at 12:03
Add a comment  | 

4 Answers 4

Reset to default 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] + ".");
}

You may find the following article interesting

  • Peeling away the jQuery wrapper and finding an array

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'}];

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));
});
//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;
 }
}
发布评论

评论列表(0)

  1. 暂无评论