1

can you tell me how to fill this element as an array

testdata = [
    {
        key: "one",
        y: 200
    },
    {
        key: "two",
        y: 300
    }
       ];

example:

testdata=[];
testdata[]="one";
testdata[]="two";
5
  • You want to create an array of objects? From what? Commented Sep 4, 2013 at 7:25
  • I want create object from array. Commented Sep 4, 2013 at 7:39
  • Could you give a better example with given input and expected output? Commented Sep 4, 2013 at 7:42
  • I I found a solution Thanks for all the help! Commented Sep 4, 2013 at 7:50
  • You mind sharing that with us? Otherwise I would advice deleting the question altogether. Commented Sep 4, 2013 at 8:06

2 Answers 2

1

It's much better to write a function that constructs a json object. Call it CreateObject and then you can just use a for loop like:

var list = [];
for(var i = 0; i < 10; i++) {
   var obj = CreateObject(i); //add other params if you need
   list.push(obj);
}
return list;
Sign up to request clarification or add additional context in comments.

Comments

0

I think you want two dimensional JS array

var items = [[11,22],[33,44],[55,66]];
alert(items[0][0]); // 11;

Or i think you want dictionary object like following

var dict = []; // create an empty array

dict.push({
    key:   "keyName",
    value: "the value"
});

reference taken from : How to create dictionary and add key value pairs dynamically in Javascript

1 Comment

I would like to be able to cycle and to fill

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.