有没有办法使用数字类型作为对象键?

发布于 2024-09-17 16:59:21 字数 374 浏览 7 评论 0原文

似乎当我使用数字类型作为对象中的键名称时,它总是会转换为字符串。有没有办法真正将其存储为数字?正常的类型转换似乎不起作用。

示例:

var userId = 1;
console.log( typeof userId ); // number
myObject[userId] = 'a value';
console.dir(myObject);

Dir 输出:

{
    '1': 'a value'
}

想要是这样的:

{
    1: 'a value'
}

建议?

It seems that when I use a numeric type as a key name in an object, it always gets converted to a string. Is there anyway to actually get it to store as a numeric? The normal typecasting does not seem to work.

Example:

var userId = 1;
console.log( typeof userId ); // number
myObject[userId] = 'a value';
console.dir(myObject);

Dir Output:

{
    '1': 'a value'
}

What I want is this:

{
    1: 'a value'
}

Advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

不语却知心 2024-09-24 16:59:21

不,这是不可能的。 密钥将始终转换为字符串。请参阅属性访问器文档

属性名称必须是字符串。这意味着非字符串对象不能用作对象中的键。任何非字符串对象(包括数字)都会通过 toString 方法类型转换为字符串。

> var foo = {}
undefined

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

> foo
{ '23213': 'swag' }

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

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'
美胚控场 2024-09-24 16:59:21

在一个对象中,没有,但我找到了 Map 对于此应用程序非常有用。这是我将其用于数字键(基于键的事件)的地方。

onKeydown(e) {
  const { toggleSidebar, next, previous } = this.props;

  const keyMapping = new Map([
    [ 83, toggleSidebar ],  // user presses the s button
    [ 37, next          ],  // user presses the right arrow
    [ 39, previous      ]   // user presses the left arrow
  ]);

  if (keyMapping.has(e.which)) {
    e.preventDefault();
    keyMapping.get(e.which)();
  }
}

In an object, no, but I have found Map extremely useful for this application. Here is where I have used it for numeric keys, a key-based event.

onKeydown(e) {
  const { toggleSidebar, next, previous } = this.props;

  const keyMapping = new Map([
    [ 83, toggleSidebar ],  // user presses the s button
    [ 37, next          ],  // user presses the right arrow
    [ 39, previous      ]   // user presses the left arrow
  ]);

  if (keyMapping.has(e.which)) {
    e.preventDefault();
    keyMapping.get(e.which)();
  }
}
宁愿没拥抱 2024-09-24 16:59:21

似乎是 ECMA-262-5 中设计的:

属性标识符类型用于将属性名称与属性描述符关联起来。属性标识符类型的值是(名称,描述符)形式的对,其中名称是字符串,描述符是属性描述符值。

但是,我在 ECMA-262-3 中没有看到明确的规范。
无论如何,我不会尝试使用非字符串作为属性名称。

Appears to be by design in ECMA-262-5:

The Property Identifier type is used to associate a property name with a Property Descriptor. Values of the Property Identifier type are pairs of the form (name, descriptor), where name is a String and descriptor is a Property Descriptor value.

However, I don't see a definite specification for it in ECMA-262-3.
Regardless, I wouldn't attempt to use non-strings as property names.

森罗 2024-09-24 16:59:21

这是解决方案。如果这不起作用,请告诉我环境设置

const screens = {
    "768": "large",
    "200": "small"
}

const keys = Object.keys(screens).map(key => parseInt(key))
                                         // OR Number(key)

console.log(keys) // Output [200, 768]

Here is the solution. Please tell me the environmental setups if this is not working

const screens = {
    "768": "large",
    "200": "small"
}

const keys = Object.keys(screens).map(key => parseInt(key))
                                         // OR Number(key)

console.log(keys) // Output [200, 768]
你的笑 2024-09-24 16:59:21

如果您想要不同的数据类型作为键,您可以使用 Map

const map1 = new Map();

map1.set(1,3)
map1.set('1','string')

// expected output: 3

console.log(map1.get(1)) //output 3;
console.log(map1.get('1')) //output 'string';

you can use, Map if you want different datatype as keys

const map1 = new Map();

map1.set(1,3)
map1.set('1','string')

// expected output: 3

console.log(map1.get(1)) //output 3;
console.log(map1.get('1')) //output 'string';
粉红×色少女 2024-09-24 16:59:21

我们需要这样的东西吗?

var userId = 1;var myObject ={};
console.log( typeof userId ); // number
myObject[userId] = 'a value';
console.dir(myObject);

安慰:
对象

1

“一个值”

Do we need something like this?

var userId = 1;var myObject ={};
console.log( typeof userId ); // number
myObject[userId] = 'a value';
console.dir(myObject);

Console:
Object

1
:
"a value"

病女 2024-09-24 16:59:21

你不能,但你总是可以将键转换为数字

const data = { 15: "value", name: "Apple" };

const result = Object.keys(data) // get keys as an array
    .map((item) => {
    return parseInt(item); // convert to integer number
    })
    .filter((item) => !isNaN(item)); // remove non number elements

    console.log(result); //Output: [15] 

You can't, but you can always convert keys to a numbers

const data = { 15: "value", name: "Apple" };

const result = Object.keys(data) // get keys as an array
    .map((item) => {
    return parseInt(item); // convert to integer number
    })
    .filter((item) => !isNaN(item)); // remove non number elements

    console.log(result); //Output: [15] 
人事已非 2024-09-24 16:59:21
const a = {
    '1': 'a value'
}
//by using a + before any string value it will convert(parse) that into a number
const b = Object.key(a);
console.log(+b); //parse
console.log(typeof b); //number
const a = {
    '1': 'a value'
}
//by using a + before any string value it will convert(parse) that into a number
const b = Object.key(a);
console.log(+b); //parse
console.log(typeof b); //number
眼睛会笑 2024-09-24 16:59:21

根据 ECMA-262 第 13 版(截至 2024 年)属性键值可以是 ECMAScript String 值或 Symbol'。

所以答案仍然是否定的,至少不是天生的。

但是,从技术上讲,可以破解它并将 32 位整数转换为 4 字符序列(字符串)并将它们用作键(请注意,这些字符串可能不是长为 4 字节)。
此方法对于大数据对象传输期间的压缩可能很有用。
例子:

'use strict';

const { log } = console;

const data = [
    { t: 1705432979, s: 124, d:   0, pl: 0 },
    { t: 1705432992, s: 125, d:  17, pl: "004500" },
    { t: 1705433022, s: 124, d:   0, pl: 0 },
    { t: 1705433330, s:  19, d: 657, pl: "deadbeef" },
];
//log("data_orig:", data);

function int2bstr(val) {
    const arr_u32 = new Uint32Array(1);
    arr_u32[0] = val;
    const arr_u8 = new Uint8Array(arr_u32.buffer);
    return String.fromCharCode(...Array.from(arr_u8));
};
function bstr2int(str) {
    if (str.length !== 4) { throw "Invalid data"; };
    const arr_u8 = new Uint8Array(4);
    const arr_u32 = new Uint32Array(arr_u8.buffer);
    for (let i=0;i<4;++i) { arr_u8[i] = str.charCodeAt(i); };
    return arr_u32[0];
};
// Converting 'Array' into 'Object' and using converted value of property 't' as key.
const data_compr = data.reduce((acc, val) => {
    const k = int2bstr(val.t);
    acc[k] = structuredClone(val);
    delete acc[k].t;
    return acc;
}, {});
log("data_compr:", data_compr);
/*
data_compr: {
    // NOTE: each key is now a 4-char string
    "\u0093צe":{ s: 124, d:   0, pl: 0 },
    " צe":     { s: 125, d:  17, pl: "004500" },
    "¾×¦e":     { s: 124, d:   0, pl: 0 },
    "òئe":     { s:  19, d: 657, pl: "deadbeef" }
}
*/

const data_decompr = Object.entries(structuredClone(data_compr)).map(([k,val]) => {
    val.t = bstr2int(k);
    return val;
});
log("data_decompr:", data_decompr);

According to ECMA-262 Edition 13 (as of 2024): A property key value is either an ECMAScript String value or a Symbol value'.

So the answer is still NO, at least not natively.

However, it is technically possible to hack it and convert 32-bit integers into 4-char sequences (strings) and use them as keys (note that those strings might not be 4-byte long).
This approach might be useful for compression during transmission of large data objects.
Example:

'use strict';

const { log } = console;

const data = [
    { t: 1705432979, s: 124, d:   0, pl: 0 },
    { t: 1705432992, s: 125, d:  17, pl: "004500" },
    { t: 1705433022, s: 124, d:   0, pl: 0 },
    { t: 1705433330, s:  19, d: 657, pl: "deadbeef" },
];
//log("data_orig:", data);

function int2bstr(val) {
    const arr_u32 = new Uint32Array(1);
    arr_u32[0] = val;
    const arr_u8 = new Uint8Array(arr_u32.buffer);
    return String.fromCharCode(...Array.from(arr_u8));
};
function bstr2int(str) {
    if (str.length !== 4) { throw "Invalid data"; };
    const arr_u8 = new Uint8Array(4);
    const arr_u32 = new Uint32Array(arr_u8.buffer);
    for (let i=0;i<4;++i) { arr_u8[i] = str.charCodeAt(i); };
    return arr_u32[0];
};
// Converting 'Array' into 'Object' and using converted value of property 't' as key.
const data_compr = data.reduce((acc, val) => {
    const k = int2bstr(val.t);
    acc[k] = structuredClone(val);
    delete acc[k].t;
    return acc;
}, {});
log("data_compr:", data_compr);
/*
data_compr: {
    // NOTE: each key is now a 4-char string
    "\u0093צe":{ s: 124, d:   0, pl: 0 },
    " צe":     { s: 125, d:  17, pl: "004500" },
    "¾×¦e":     { s: 124, d:   0, pl: 0 },
    "òئe":     { s:  19, d: 657, pl: "deadbeef" }
}
*/

const data_decompr = Object.entries(structuredClone(data_compr)).map(([k,val]) => {
    val.t = bstr2int(k);
    return val;
});
log("data_decompr:", data_decompr);

_畞蕅 2024-09-24 16:59:21

根据 Mozilla:
传播语法

let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };
const merge = ( ...objects ) => ( { ...objects } );

let mergedObj1 = merge (obj1, obj2);
// Object { 0: { foo: 'bar', x: 42 }, 1: { foo: 'baz', y: 13 } }

let mergedObj2 = merge ({}, obj1, obj2);
// Object { 0: {}, 1: { foo: 'bar', x: 42 }, 2: { foo: 'baz', y: 13 } }

console.log(mergedObj1);
console.log(mergedObj2);

只需提前订购商品,您就应该得到您想要的结果。

所以对于你的情况:

const merge = (...objects) => ({...objects});

// An object with numeric keys
const values = ["a value", "another value", "and another value"];
        
let merged = merge(...values);

console.log(merged);

Per Mozilla:
Spread syntax

let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };
const merge = ( ...objects ) => ( { ...objects } );

let mergedObj1 = merge (obj1, obj2);
// Object { 0: { foo: 'bar', x: 42 }, 1: { foo: 'baz', y: 13 } }

let mergedObj2 = merge ({}, obj1, obj2);
// Object { 0: {}, 1: { foo: 'bar', x: 42 }, 2: { foo: 'baz', y: 13 } }

console.log(mergedObj1);
console.log(mergedObj2);

Just order the items before hand and you should get the result you want.

So for your case:

const merge = (...objects) => ({...objects});

// An object with numeric keys
const values = ["a value", "another value", "and another value"];
        
let merged = merge(...values);

console.log(merged);

仙女山的月亮 2024-09-24 16:59:21

你可以试试这个:

arr = {}
function f(a,b,c) {
      arr = arguments
}
f("*","#","_")
console.log(arr)
//returns Object { 0: "*", 1: "#", 2: "_" }```

You can try this:

arr = {}
function f(a,b,c) {
      arr = arguments
}
f("*","#","_")
console.log(arr)
//returns Object { 0: "*", 1: "#", 2: "_" }```
沫尐诺 2024-09-24 16:59:21

在 JavaScript 中,数字字符串和数字是可以互换的,因此

myObject[1] == myObject['1']

如果您确实希望数字成为对象的键,您可能需要一个数组(即使用 new Array()[] 创建) )。

In JavaScript, numerical strings and numbers are interchangeable, so

myObject[1] == myObject['1']

If you really want number to be the key for an object, you might want an array (i.e. created with new Array() or []).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文