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

197
Vistas
Is there a way to get the keys in a dictionary as their original type?

I am very new to typescript and I have the following dictionary where the the keys and values are float arrays:

start_to_end_dict
> {-121.95131592,37.253239074: Array(2)}
> -121.95131592,37.253239074: (2) [-131.950349087, 47.253099466]
> [[Prototype]]: Object

I want to get a list of the keys as a list of arrays like this:

> [Array(2)]
> 0: (2) [-121.95131592, 37.253239074]
> length: 1
> [[Prototype]]: Array(0)

But then I get a list of strings instead:

Object.keys(start_to_end_dict)
['-121.95131592,37.253239074']

I noticed that values seems to get a list of arrays:

Object.values(start_to_end_dict)
> [Array(2)]
> 0: (2) [-131.950349087, 47.253099466]
> length: 1
> [[Prototype]]: Array(0)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this :

const start_to_end_dict = {
  '-121.95131592,37.253239074': [-131.950349087, 47.253099466]
};

const arr = [];

Object.keys(start_to_end_dict).forEach(key => {
    key.split(',').forEach(elem => arr.push(Number(elem)))
});

console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

As noted by other users in comments on your question: keys of objects are always strings in JavaScript. You can write a function to parse the string coordinates, and then iterate over the entries of the object, using it to parse the numeric coordinate values from each key, while accessing the (already parsed) values directly:

An explanation of the regular expression below can be accessed here.

TS Playground

type Coords = [number, number];

const coordsRegex = /^\s*(?<n1>-?(?:\d+\.)*\d+)\s*,\s*(?<n2>-?(?:\d+\.)*\d+)\s*$/;

function parseCoords (stringCoords: string): Coords {
  const groups = stringCoords.match(coordsRegex)?.groups;
  if (!groups) throw new Error('Coordinates format not valid');
  const coords = [groups.n1, groups.n2].map(Number) as Coords;
  return coords;
}

// This is the object value that you showed in the question:
const exampleData: Record<string, Coords> = {
  '-121.95131592,37.253239074': [-131.950349087, 47.253099466],
};

for (const [key, value] of Object.entries(exampleData)) {
  const keyCoords = parseCoords(key);
  console.log({keyCoords, valueCoords: value});
}

Compiled JS from the TS playground above:

"use strict";
const coordsRegex = /^\s*(?<n1>-?(?:\d+\.)*\d+)\s*,\s*(?<n2>-?(?:\d+\.)*\d+)\s*$/;
function parseCoords(stringCoords) {
    const groups = stringCoords.match(coordsRegex)?.groups;
    if (!groups)
        throw new Error('Coordinates format not valid');
    const coords = [groups.n1, groups.n2].map(Number);
    return coords;
}
// This is the object value that you showed in the question:
const exampleData = {
    '-121.95131592,37.253239074': [-131.950349087, 47.253099466],
};
for (const [key, value] of Object.entries(exampleData)) {
    const keyCoords = parseCoords(key);
    console.log({ keyCoords, valueCoords: value });
}

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