typescript - 对象属性应该是整数但是字符串?

问题描述 投票:-1回答:1

我需要遍历一个对象qazxsw poi并找到特定值的关键索引。

nodeIDs

返回的键是数字..现在我构建了一个新对象,其中保存了键索引

 private getNodeIndexByID(nodeIDs, id) {
   for (const [key, value] of Object.entries(nodeIDs)) {
     if (id === value) {
       return key;
     }
   }
 }

现在const index_source = this.getNodeIndexByID(nodeIDs, obj.source); const index_target = this.getNodeIndexByID(nodeIDs, obj.target); let my_obj = Object.create({}, { source: { value: index_source }, target: { value: index_target } }); out.push(my_obj); out.source的值是STRO的类型..为什么? ..我的意思是,数组索引是数字..我错过了什么? ..我需要他们是数字。

javascript arrays typescript object types
1个回答
1
投票

JavaScript对象的键总是(总是!)字符串,即使它们是使用数字键写入的。

换一种说法,

out.target

与...完全相同

x[0] = 1

如果您正在计算对象的键/值对,您将看到字符串键,因为JavaScript对象的键始终是字符串。

© www.soinside.com 2019 - 2024. All rights reserved.