2

I need to create the following array dynamically, for example:

var data = { point: [
              { x:5, y:8 },
              { x:8, y:10},
              ]};

 console.log(data.point[0].x);   // 5  n=0
 console.log(data.point[1].y);   // 10 n=1

At some point my application needs to expand the array to more than 2 items (n=0, n=1). Please let me know how to do that (i.e. n = 9 ).

2
  • 3
    Just assign it. If you just want to insert at the back, use push(). If you want to insert at front, use unshift(). Commented Jul 17, 2012 at 3:48
  • use loop to iterate on the conditions you want to work with Commented Jul 17, 2012 at 3:48

4 Answers 4

2

You could use Array.push method to add element to an array.

var point = {x:1,y:1};
data.point.push(point);
Sign up to request clarification or add additional context in comments.

Comments

1

you can use method 'push' like this code

        var data = { point: [
                      { x:5, y:8 },
                      { x:8, y:10},
                      ]};

         console.log(data.point[0].x);   // 5  n=0
         console.log(data.point[1].y);   // 10 n=1

        data.point.push({x:4,y:3});

        console.log(JSON.stringify(data.point));

Comments

0

You could do something like this:

var data = {'points' : []};

function addPoint(x, y) {
    data.points.push({'x' : x, 'y' : y});
}

function getPoint(index) {
    return data.points[index];
}

addPoint(10, 20);
addPoint(5, 3);

var p = getPoint(1);
alert(p.x + ", " + p.y); //alerts => "5, 3"

This would work for any arbitrary number of points.

Update

To clear the array

function clearPoints() {
    data.points = [];
}

Update 2

These little functions will work okay if you have a simple page. If this points handling is going to end up being part of a larger system, it may be better to do something like this:

var data = {
    'points' : [], 
    addPoint : function(x, y) {
        this.points.push({
            'x' : x, 
            'y' : y
        });
    }, 
    getPoint : function(index) {
        return this.points[index];
    }, 
    clearPoints : function() {
        this.points = [];
    }, 
    removePoint : function(index) {
        this.points.splice(index, 1);
    }
};

Example usage:

alert(data.points.length); // => 0

data.addPoint(1, 2);
data.addPoint(8, 12);
data.addPoint(3, 7);
alert(data.points.length); // => 3

var p = data.getPoint(2); //p = {x:3, y:7};

data.removePoint(1); 
alert(data.points.length); // => 2

data.clearPoints();
alert(data.points.length); // => 0

It may allow you to keep your point handling a little cleaner and easier to use and update.

6 Comments

Thanks jwatts1980, interesting....after calling addPoint(x,y) for number of times. For example, 5 times. At some point, I would like to reset (clear) the array and start from the beginning (i.e the array 'data' has no element in it). How could I do that?
Excellent...Thank you. I will try it and let you know tomorrow.
Update 2 got syntax error at addPoint, I put missing ')', but still not working....
Whoops. My fault. I put equal signs instead of colons. Corrected. Tested. Works.
Thanks..what about "this.points.push({..." does not have closing ")"?
|
0

You can either use the push() function as stated, or add additional items to the array using an index, which is preferred in the Google Style Guide. The latter method does require that you have a pointer to the last index.

Note that since assigning values to an array is faster than using push() you should use assignment where possible.

for(var i=lastIndex; i < numOfNewPoints; i++){
    data.point[i] = {x:4, y:3};
}

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.