JavaScript - Swapping out element in object array

I'm struggling to develop a function that can normalize features within a dataset, ensuring they fall between 0 and 1. My goal is to iterate through all the features and update their values as I perform normalization. While the normalization process itself is successful, I am facing challenges in replacing the original values with the normalized ones.

Data.prototype.normalize = function(dataset) {

    // Calculate 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];

        // Normalize the feature values for all companies using the calculated extent
        for (var company = 0; company < this.companies.length; company++) {
            var value      = this.companies[company][dataset][feature],
                normalized = this.normalizeValue(value, min, max);

            value = normalized;
        }
    }

}

The issue arises at

value = normalized;

While logging the updated value shows success within the function scope, the changes do not persist outside of it.

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

An example of the feature array within my main object is provided above. Any suggestions on resolving this problem would be greatly appreciated.

Thank you!

Answer №1

To ensure that the change is applied to your object instead of the function, you must explicitly define the property of your object.

Adjust your for loop to specify the normalized value like this:

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; // set normalized value explicitly
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show or hide the right element when clicked using ng-show in AngularJS

My goal is to toggle elements based on which one is clicked, not all at once. For instance, if I have 3 form elements and 3 buttons, when I click on button 1, I only want to toggle the corresponding form element. Here is my current code snippet: In Angu ...

Verify using JavaScript whether the call is originating from a specific JSP

My global.js file serves as a common JavaScript file across my project. I recently added some code to a function named 'test' in this file. However, I only want this code to run when the function is called from a specific JSP file called home.j ...

Working with dynamic array sizes in methods and setters in PHP

I am relatively new to PHP and have been researching extensively, but I am struggling to find a solution to my current issue. My problem revolves around using setters in a class when dealing with an array of unknown size. For example: class Shirt { ...

Reset the input field once the message has been successfully sent

My goal is to clear the form message input field after the form is sent. However, it seems to be clearing the data before it has a chance to be sent. Here is the code I'm using: <script> $(function () { $('form#SendForm'). ...

How to remove an item from the shopping cart and use a discount code with JavaScript

I need help with removing an item from the cart without deleting the entire cart, and also adjusting the price if the customer has a coupon or exceeds a certain quantity. I am working on this using JavaScript before implementing it in Django. Here is the H ...

Save File to Local Storage

I've utilized the Java DOM to make modifications to an XML template and now aim to save the resulting Document in the private internal storage of an Android device, following the guidelines provided in the official API documentation. My progress so f ...

Error encountered during login with Facebook on Cordova: "Unable to create module facebookApp because of..."

As a novice programmer, I am attempting to incorporate Facebook login using Cordova. Following a recent guide provided here, I have meticulously copied and pasted the code into a new Cordova project. While I am familiar with jQuery, this is my first encoun ...

Vue application experiencing never-ending update cycle following array assignment

Here is the JavaScript code I am working with: const storage = new Vue({ el: '#full-table', delimiters: ['[[', ']]'], data: { events: [], counter: 0, }, methods: { eventCounter: fu ...

Establishing communication from a node.js application to an Apache server with version 2.2

Each time I call the function dorequest during a request to my node server, I encounter an issue with requests to a webpage hosted on apache2.2.21. While most of the requests are successful, I am getting an error ECONNRESET on a few of them, and I am uns ...

Is there a way to highlight today's working hours with a different color using Vue.js?

Here is the script I have created to display the working hours: const workHour = "Monday :9:00AM to 5:00PM,Thursday :9:00AM to 5:00PM,Wednesday :9:00AM to 5:00PM,Tuesday : 9:00AM to 5:00PM,Friday :9:00AM to 5:00PM,Saturday :9:00AM to 5:00PM,Sunday :9:00AM ...

The JavaScript code created in CodePen doesn't seem to function properly when integrated into my website

During a learning program, I created a basic random quote generator. The code worked flawlessly on codepen.io. Using a simple JavaScript (jQuery) function, I displayed random quotes along with their authors, which were stored in an array. I ensured that t ...

What if there was a magical jQuery method that could automatically trigger a callback function? What could it possibly be named?

Is there a way to load only images and iframes, similar to the .load() function? I want to automatically add a selector element into the "this" variable for this purpose. $('document').ready(function({ $('a').<Something to trigg ...

Having trouble setting up a twitter widget?

I have developed a custom jQuery plugin that displays a sidebar on the left for social media content. The variable below contains the source of the content. I am currently facing an issue while trying to integrate a Twitter feed using the widget, as it kee ...

Utilize HTML5 and Javascript to easily upload files from your desktop through the drag and drop feature

I have created a drag and drop functionality for uploading images from the desktop to the browser. When I drop the image inside the designated box, I can view the image details in the browser console: File { name: "deam230mthumb.jpg", lastModified: 119464 ...

Navigating to an element using Selenium and controlling Firefox with Node.js

Seeking guidance on how to scroll to and click a link element on the page. The current solution works for Chrome and IE, but Firefox gives an error. Any suggestions on fixing this or alternative approaches? function clickByLinkTextScroll(text){ d ...

add multiple arrays to a new empty array using PHP

I am trying to create a function that will retrieve the children of a parent. The function get_children is intended to return an array of children with the ID $this_parent_id. /* $pages array */ Array ( [0] => Pages Object ( [i ...

Error: The call stack has reached its maximum size while running an npm install

Attempting to execute npm install, encountered the following console output: npm ERR! Linux 4.8.0-27-generic npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" npm ERR! node v6.9.1 npm ERR! npm v3.10.8 npm ERR! Maximum call stack size exceeded npm ...

Leveraging "setState" in React Native with Promises

I am facing a challenge in updating the state of a React Component with data obtained from an API call. I encountered an issue where using the setState function within the promise resulted in an error "Type Error: _this2.setState is not a function" in Reac ...

Adding a navigation bar to every administrator page while excluding it from shop pages can be achieved by creating a

I am facing a challenge in implementing a navbar and sidebar on all admin pages, except for the shop page. The issue arises because I have set my sidebar and navbar to be global. My goal is to make them global only for admin pages and not for the shop. He ...

Tips for invoking the setState method via an onClick event the ES6 way

class BlogPost extends React.Component{ //getInitialState constructor(){ super(); this.onLike = this.onLike.bind(this); this.state = { like :0 } } onLike(){ this.setState({ li ...