JavaScript - 替换对象中的数组值
JavaScript - Replacing array value within object
我正在尝试编写一个函数来规范化数据集中的特征(介于 0 和 1 之间)。我想在规范化时遍历所有功能和 replace 值。规范化效果很好,但我无法覆盖以前的值。
Data.prototype.normalize = function(dataset) {
// Get the extent for each feature
for (var feature = 0; feature < this.featureCount; feature++) {
var extent = this.getExtent(feature, dataset),
min = extent[0],
max = extent[1];
// uses extent to normalize feature for all companies
for (var company = 0; company < this.companies.length; company++) {
var value = this.companies[company][dataset][feature],
normalized = this.normalizeValue(value, min, max);
value = normalized;
}
}
}
在
一切都失败了
value = normalized;
如果我 console.log(value) 覆盖它之后一切似乎都有效,但仅限于函数范围内。在此范围之外,原始值保持不变。
data.companies[n] = { features : [1, 2, 3, 4, 5], other properties... }
这是我的主要对象中特征数组的示例。
关于如何解决这个问题有什么想法吗?
谢谢!
为了使更改反映在对象而不是函数中,您需要明确设置对象的 属性。
修改您的 for
循环以显式设置标准化值,如下所示:
for (var company = 0; company < this.companies.length; company++) {
var value = this.companies[company][dataset][feature],
normalized = this.normalizeValue(value, min, max);
this.companies[company][dataset][feature] = normalized; // explicitly set value
}
我正在尝试编写一个函数来规范化数据集中的特征(介于 0 和 1 之间)。我想在规范化时遍历所有功能和 replace 值。规范化效果很好,但我无法覆盖以前的值。
Data.prototype.normalize = function(dataset) {
// Get the extent for each feature
for (var feature = 0; feature < this.featureCount; feature++) {
var extent = this.getExtent(feature, dataset),
min = extent[0],
max = extent[1];
// uses extent to normalize feature for all companies
for (var company = 0; company < this.companies.length; company++) {
var value = this.companies[company][dataset][feature],
normalized = this.normalizeValue(value, min, max);
value = normalized;
}
}
}
在
一切都失败了value = normalized;
如果我 console.log(value) 覆盖它之后一切似乎都有效,但仅限于函数范围内。在此范围之外,原始值保持不变。
data.companies[n] = { features : [1, 2, 3, 4, 5], other properties... }
这是我的主要对象中特征数组的示例。
关于如何解决这个问题有什么想法吗?
谢谢!
为了使更改反映在对象而不是函数中,您需要明确设置对象的 属性。
修改您的 for
循环以显式设置标准化值,如下所示:
for (var company = 0; company < this.companies.length; company++) {
var value = this.companies[company][dataset][feature],
normalized = this.normalizeValue(value, min, max);
this.companies[company][dataset][feature] = normalized; // explicitly set value
}