我需要遍历一个对象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对象的键始终是字符串。