How to Use a Numeric Type as an Object Key

Is there any way to use a numeric type as an object key?

No, this is not possible. The key will always be converted to a string. See Property Accessor docs

Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.

> var foo = {}
undefined

> foo[23213] = 'swag'
'swag'

> foo
{ '23213': 'swag' }

> typeof(Object.keys(foo)[0])
'string'

Object.keys using numbers in typescript

All keys are strings in JavaScript - just use map:

const sizes: number[] = Object.keys(foo).map(Number);

This'll only work for numeric strings - if it's a European string involving decimals, for instance, it'll be NaN, and that never ends well.

console.log(Number("10,7"));

Is it bad to have number as object keys?

Its fine, as in - its legal, as its how arrays work, though one thing to watch out for is that arrays will ensure the key is incremental, starting at 0, whereas a numerical key in an object will not, and WILL be misleading for other developers when they cant understand why obj[1] doesnt always live beside obj[2] etc, and by that I mean the use of within loops etc and simply incrementing the index by one each time. So, personally i wouldnt recommend it due to it looking like an array, but it not actually being an array...

Side - If you are trying to mimic an array, but want an index starting at 1, just use an array as normal and have your code just handle the offset each time. Arrays come with many functions, like length, sort, find etc that an object will not

Why does a number as key worked for a string key in an Object and vice-versa (Javascript)

The relevant bits of the standard are

12.3.2.1 Runtime Semantics:
Evaluation

MemberExpression:MemberExpression[Expression]

...6. Let propertyKey be ? ToPropertyKey(propertyNameValue).

and

7.1.14 ToPropertyKey ( argument )

  1. Let key be ? ToPrimitive(argument, hint String).

  2. If Type(key) is Symbol, then Return key.

  3. Return ! ToString(key).

In plain English, in object[whatever], whatever is converted to a string, unless it's a symbol.

Illustration:

let s = Symbol();
let o = { '12': 1, 'foo': 2, 'true': 3, [s]: 4}
console.log(o[6*2])console.log(o[{toString: () => 'foo'}])console.log(o[1 === 1])console.log(o[s])

Enforcing the type of the indexed members of a Typescript object?

var stuff: { [key: string]: string; } = {};
stuff['a'] = ''; // ok
stuff['a'] = 4; // error

// ... or, if you're using this a lot and don't want to type so much ...
interface StringMap { [key: string]: string; }
var stuff2: StringMap = { };
// same as above

Object property name as number

You can reference the object's properties as you would an array and use either me[123] or me["123"]

Get object value that has numeric key in react native

I am not a React Native guy but this seems more like a JavaScript syntax nuance.

You can access object keys as obj['key']

Here, in your case, it will be

aboveObject.Content['1']['2']

Updating answer after OP's edit to the question

const first_numeric_key = 1;
const second_numeric_key = 2;
aboveObject.Content[first_numeric_key.toString()][second_numeric_key.toString()];

EDIT

This is working without convert to the string

aboveObject.Content[first_numeric_key][second_numeric_key]

The issue was the exception handling, since there was some delaying when the app get the data from the fetch() function so the aboveObject was null at the very first time.



Related Topics



Leave a reply



Submit