1

I want to create a JavasScript array in following format:

var datas = [
    { name: "Peter Pan", location: "[email protected]" },
    { name: "Molly", location: "[email protected]" },
    { name: "Forneria Marconi", location: "[email protected]" },
    { name: "Master <em>Sync</em>", location: "[email protected]" }
];

As I want to create it dynamically, It would be great if I can create an array like that.

1
  • 3
    That code you posted does create a JavaScript array. What do you mean "dynamically"? Commented Jun 14, 2013 at 11:43

5 Answers 5

3

You just did it.

But if you mean adding objects one at a time you can do

var datas = [];

and then

newobj = //dynamically created object

datas.push(newobj);
Sign up to request clarification or add additional context in comments.

Comments

2
var data = [];
data.push({ name: "Peter Pan", location: "[email protected]" },
    { name: "Molly", location: "[email protected]" },
    { name: "Forneria Marconi", location: "[email protected]" },
    { name: "Master <em>Sync</em>", location: "[email protected]" });

If you are receiving the object from some service, just call data.push(objectName);. You will have array of objects.

Fiddle for you. http://jsfiddle.net/q69ku/

Comments

2

Have a look at: How do I create JavaScript array (JSON format) dynamically?

var data = [];
data.push({ name: "Peter Pan", location: "[email protected]" });
// ...

Comments

2

data.push({name: "x" location:"y"})

Comments

2

you will need to change this to fit your data results

 var arr = [];
    var b = 20; // or your data results
    for(i=0;i<b;i++){
        var obj = {name: "Peter Pan", location: "[email protected]"};
    arr.push(obj);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.