0

I have not that much experience in javascript so I dont know how I can exdent the array automatically. Here is an example:

 var chart_arr = [
    {
        "x": 1325676960000,
        "y": 25500.50,
    },
    {
        "x": 1325875140000,
        "y": 30000.50,
    }
];

So now I want to add more fields to it. How can I achieve this?

4
  • 1
    That isn't a multi-dimensional array. And "adding fields" is meaningless. Are you asking how to set properties inside a loop? Commented Sep 17, 2014 at 20:10
  • just push a new object to it Commented Sep 17, 2014 at 20:10
  • 1
    Kruben, have a look at this documentation about the JavaScript Array. That should help you on the way. Commented Sep 17, 2014 at 20:15
  • 1
    A really great resource for language questions like this would be the Mozilla developer network: developer.mozilla.org/en-US/docs/Web/JavaScript they have great basic tutorials as well as feature documentation. If it's not already in your bookmarks for the first place to find info on JS, then you need to add it. Commented Sep 17, 2014 at 20:17

2 Answers 2

2
chart_arr[0].z = 12345; // add a new property to an existing object
chart_arr.push({x:567, y:890}); // add a new object to the array

As an aside, you don't have a two-dimensional array, you have an array of objects.

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

2 Comments

Thanks, dude. I think I have demonstrate that i hadnt much knowledge about it. thanks a lot.
Not to worry. We were all a newbie at the beginning. Welcome to StackOverflow.
0

This is another way of doing it:

Object.defineProperty(chart_arr[0],"z",{ value: 1, writable:true,enumerable:true,configurable:true});

This defines z value as 1 into chart_arr[0].

Result:

chart_arr[0] -> x: 1325676960000 y: 25500.5 z: 1

chart_arr[1] -> x: 1325875140000 y: 30000.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.