Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
How to convert string to a number in a two-dimensional array?

So i did write a simple function to deal with my string declared in variable poly, as you can see I used a split() method and now i want to convert each of those string values to numerical values:

function toArray(polygon) {
  final = polygon.replace('POLYGON ', '').replace('((', '').replace('))', '').split(',');

  const arrOfNum = [];

  final.forEach(str => {
    arrOfNum.push(Number(str));
  });
  return arrOfNum
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

I'm trying to convert strings to a numerical values but I'm getting this result:

    [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]

Later on i want to get to this exact point:

    [[20.7218472,52.2294069],
    [20.9436337,52.0756329],
    [21.0651699,52.2134223],
    [20.7767788,52.2537934],
    [20.7218472,52.2294069]]

The main goal of those operations is that i want to use this data to find out if a Point is within a Polygon. To do this I'm using this function:

function ray_casting(point, polygon){
    var n=polygon.length,
        is_in=false,
        x=point[0],
        y=point[1],
        x1,x2,y1,y2;

    for(var i=0; i < n-1; ++i){
        x1=polygon[i][0];
        x2=polygon[i+1][0];
        y1=polygon[i][1];
        y2=polygon[i+1][1];

        if(y < y1 != y < y2 && x < (x2-x1) * (y-y1) / (y2-y1) + x1){
            is_in=!is_in;
        }
    }

    return is_in;
}

Thx everyone for the help!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This solution is equivalent to one of @mplungjan's. Another approach would be to use new Function or just Function as in the second demo.

const poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

const nums = poly.replace(/POLYGON \(\(|\)\)/g, '')
.split(/, /).map(num => num.split(/ /).map(n => +n));

console.log( nums );

USING new Function()

const poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))',

      jstr = poly
             .replace(/POLYGON \(\(/, '[[')
             .replace(/\)\)/, ']]')
             .replace(/, /g, '],[')
             .replace(/ /g, ','),
             
      output = new Function(`return ${jstr}`)();

console.log( output );

about 4 years ago · Juan Pablo Isaza Denunciar

0

You missed some spaces and need to handle the pairs

Regex

function toArray(polygon) {
  const final = polygon.match(/(\d+\.\d+ \d+\.\d+)/g)
  return final.flatMap(str => ([str.split(" ").map(str => +str)]));
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

Your version extended

function toArray(polygon) {
  const final = polygon
  .replace('POLYGON ((', '')
  .replace('))', '')
  .split(", ");

  return final.flatMap(str => ([str.split(" ").map(str => +str)]));
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simply achieve it by just simply splitting the array element and then typecast into a Number.

Demo :

function toArray(polygon) {
  final = polygon.replace('POLYGON ', '').replace('((', '').replace('))', '').split(',');

  const arrOfNum = [];

  final.forEach(str => {
    const arr = [];
    str.trim().split(' ').forEach(str => arr.push(Number(str)))
    arrOfNum.push(arr);
  });
  return arrOfNum
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda