typescript - 对象属性应该是整数但是字符串?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript - 对象属性应该是整数但是字符串?相关的知识,希望对你有一定的参考价值。
我需要遍历一个对象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对象的键总是(总是!)字符串,即使它们是使用数字键写入的。
换一种说法,
out.target
与...完全相同
x[0] = 1
如果您正在计算对象的键/值对,您将看到字符串键,因为JavaScript对象的键始终是字符串。
以上是关于typescript - 对象属性应该是整数但是字符串?的主要内容,如果未能解决你的问题,请参考以下文章