11

Hi I need some help with javascript.

function PricingData(id,method,freq,service,price) {
            this.ID=id;
            this.PaymentMethod_ID=method;
            this.PaymentFrequency_ID=freq;
            this.Service_ID=service;
            this.Price=price;
        }

I need to create an array in this way.

var temp=new PricingData[]{new PricingData(1,2,3,4,5),new PricingData(1,2,3,4,5)};

but this doesn't work. I'm going to pass the data in through the server so I would prefer syntax similar to this

1
  • 2
    I am not sure what you mean by pass the data in through the server, but I'd suggest you may want to look at json. json.org You may also want to look at jquery for transferring your data between the server and browser jquery.Ajax() Commented Feb 25, 2011 at 9:59

3 Answers 3

14

Use array literal notation to create an array:

var tmp = [new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5)];

For more information about arrays have a look at MDC - Array.

Sign up to request clarification or add additional context in comments.

6 Comments

just out of curiosity, when an array is declared like this, how do you reference it?
@david: tmp is a reference to the array. tmp[i] gives you the ith element. Is that what you wanted to know?
Ah - sort of, I meant as in can I do $var price= tmp[i].Price (to return the price), thank you
@David: Yes, you can do that (if you remove the $, it's not PHP ;))
how to create this one dynamicaly ?
|
4

just put the new object right between the square brakcets

var arr = [new whatever(), new whatever()];

Comments

0

That's how to create an array of custom object by using this syntax:

var myObjArr = new Array(new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5));

or

var myObjArr = [new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5)];

Comments

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.