Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda