1

I'm trying to write a function that normalizes features (between 0 and 1) in a dataset. I want to iterate through all the features and replace values as I normalize them. Normalization works well, but I'm unable to overwrite the previous value.

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;
        }
    }

}

It all fails at

value = normalized;

If I console.log(value) after overwriting it everything seems to work, but only within the scope of the function. Outside of this scope the original value remains.

data.companies[n] = { features : [1, 2, 3, 4, 5], other properties... }

Here is an example of the feature array within my main object.

Any thoughts on how to fix this?

Thank you!

1
  • Primitive types are never passed by reference in Javascript. Commented Nov 28, 2017 at 21:09

1 Answer 1

5

In order for the change to be reflected in your object as opposed to the function, you need to explicitly set the property of your object.

Modify your for loop to explicitly set the normalized value like so:

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice psychic debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.