Home > Blockchain >  Is there a way to get the keys in a dictionary as their original type?
Is there a way to get the keys in a dictionary as their original type?

Time:05-05

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)

CodePudding user response:

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 });
}

CodePudding user response:

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);

  • Related