1

I want to create some objects like object_point1, object_point2, ... with a for loop that split a string with x and y coords. How can i use iterates to create the name of objects? Thanks

var vMsg = req.body.myMessage;
var fields = vMsg.split(/\n/);      

var myobjct = new Object();
myobject.PointCount=parseFloat(paramsCoords);

for (var ii=0; ii<fields.length; ii++)
{
  var coord=fields[ii].split(/\t/);
  //console.info ("X" + coord[0]);
  //console.info ("Y" + coord[1]);

var object_Point[ii] = new Object();
object_Point[ii].x_m=parseFloat(coord[0]);
object_Point[ii].y_m=parseFloat(coord[1]);
myobject.Polygon_Point[ii]=object_Point[ii];
}

At the moment i use this construction:

for (var ii=0; ii

var coord=fields[ii].split(/\t/);

var objPolygon_Point = new Object()
objPolygon_Point["point" + ii] = new Object();
objPolygon_Point["point" + ii].x_m=parseFloat(coord[0]);
objPolygon_Point["point" + ii].y_m=parseFloat(coord[1]);

if (ii=='1')
{
myobject.Polygon_Point1=objPolygon_Point["point" + ii];
}
if (ii=='2')
{
myobject.Polygon_Point2=objPolygon_Point["point" + ii];
}
// ii==3, ii==4, .......
}
rennsau
  • 21
  • 2

2 Answers2

2

You can generate dynamic object names in the global scope like:

Browser:

var ii = 11
    , x = 123
    , y = 234;

window['Object_Point' + ii] = {
    x : parseFloat(x),
    y : parseFloat(y)
}

console.log(Object_Point11);
console.log(window.Object_Point11);

// Object {x: 123, y: 234}

node.js

> var i = 12;
> global['MyObj'+i] = { hello : 'world' };

> console.log(MyObj12);
> console.log(global.MyObj12);
// { hello: 'world' }

see node.js global variables?

But rather than using window or global, you might want to use your own object

> var i = 12, myObj = {};
> myObj['MyObj'+i] = { hello : 'world' };
> console.log(myObj.MyObj12);
// { hello: 'world' }
Community
  • 1
  • 1
gherkins
  • 14,603
  • 6
  • 44
  • 70
0

I directly used your example. I'll suggest to create middle-map object. I.e. something like holder of all the points. Using the global namespace is not a good practice.

var vMsg = req.body.myMessage;
var fields = vMsg.split(/\n/);      

var myobjct = new Object();
myobject.PointCount=parseFloat(paramsCoords);

var points = {};

for (var ii=0; ii<fields.length; ii++)
{
    var coord=fields[ii].split(/\t/);
    //console.info ("X" + coord[0]);
    //console.info ("Y" + coord[1]);
    var point = points["point" + ii] = new Object();
    point.x_m = parseFloat(coord[0]);
    point.y_m = parseFloat(coord[1]);
    myobject.Polygon_Point[ii] = point;
}
Krasimir
  • 13,306
  • 3
  • 40
  • 55
  • The last line with myobject.Polygon_Point[ii] = point; throws an error: Cannot set property '0' of undefined – rennsau Aug 20 '13 at 12:18
  • Ok, can you please give more information about what exactly Polygon_Point is. Is it an array or it is an object. – Krasimir Aug 20 '13 at 12:48
  • Thanks for your answer. Its an object to write the coords in a json file. For the Moment i use an ugly construction, but it works. I`ve edit the code in the start post. – rennsau Aug 22 '13 at 07:46