Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!