Changing the value of undefined properties in a JavaScript object

I am struggling to identify and replace null values with 0's in my object. I was able to access the correct member within the loop, but once it exited, the values were no longer assigned as 0. Working with objects in JavaScript is new to me, so I'm feeling pretty lost. Any assistance on this matter would be greatly appreciated.

var data = { 
    0 : {
         Day1: {
               Hours: 6,
               Minutes: null
         },
         Day2: {
               Minutes: 45
         },
         Day3: {
               Hours: 8,
               Minutes: 15
         },
    1 : {
         Day1: {
               Hours: 6,
               Minutes: 20
         },
         Day2: {
               Hours: 45,
               Minutes: null
         },
         Day3: {
               Hours: 8,
               Minutes: 15
         }
};


for (var item in data) {
    for (var item2 in item) {
         item[item2].Hours = item[item2].Hours || 0;
         item[item2].Minutes = item[item2].Minutes || 0;
    }
}

//Ignore this line. Just assigning onject to angular scope when finished
$scope.timeInfo = data;

Answer №1

When working with for (variable in object) loops, it's important to note that the variable refers to the key property names of the object being iterated. In your specific case, one possible solution to achieve your goal is outlined below:

(Please take note that I have included additional braces in your data object for better structure.)

var data = { 
    0 : {
         Day1: {
               Hours: 6,
               Minutes: null
         },
         Day2: {
               Minutes: 45
         },
         Day3: {
               Hours: 8,
               Minutes: 15
         }
    },
    1 : {
         Day1: {
               Hours: 6,
               Minutes: 20
         },
         Day2: {
               Hours: 45,
               Minutes: null
         },
         Day3: {
               Hours: 8,
               Minutes: 15
         }
    }
};

for (var item in data) {
    for (var item2 in data[item]) {
         data[item][item2].Hours = data[item][item2].Hours || 0;
         data[item][item2].Minutes = data[item][item2].Minutes || 0;
    }
}

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

Utilize AngularJS to Capitalize the First Letter and the Letter Following a Dot (.)

I am seeking a solution to capitalize the first letter in a given string, like so: sumo => Sumo Additionally, I need to capitalize strings in the following format: Dr.ravi kumar => Dr.Ravi kumar Although I have implemented an Angular filter that ...

Exploring the best practices for organizing logic within Node.js Express routes

I'm currently utilizing the https://github.com/diegohaz/rest/ boilerplate, but I am unsure about the best practice for implementing logic such as QR code generation and additional validation. My initial thought was to include validation and password ...

The Ajax/jQuery output is not showing up in the iframe, but is instead appearing on a separate page

I've scoured various forums and websites, attempting to troubleshoot the issue described below without success. This problem persists in both IE 11 and Firefox 10.0.2. My goal is to submit a form (POST) to a website () and showcase the resulting bri ...

What strategies can I use to optimize the code for the function provided?

This function allows for the dynamic display of select tags for location selection based on employee designation. The current code functions correctly, but I believe it can be optimized further. // Function to activate different location options based on e ...

How can one conditionally UPDATE/INSERT data following a DELETE operation that does not return any matching rows?

I'm currently working on updating a table after deleting a value from a different table. Below is the simplified function query I am using for this purpose: create function updateoutfit(_id uuid, _title text DEFAULT NULL::text, _garments json) re ...

Creating one div to initiate the contents to fadeToggle and another div to cease the fadeToggle animation utilizing the setInterval function in JavaScript

Here is the link to my Fiddle: http://jsfiddle.net/tmanundercover/62ap2/ CSS #targetedDiv, #otherDiv { border: 1px solid; } HTML <div id="targetedDiv">&nbsp<span id="fakeCursor">|</span></div> <br> <div id="othe ...

Display the Meteor Simple Schema for Entities

I've created a Meteor application and set up some database collections with SimpleSchema from https://github.com/aldeed/simple-schema-js. Cards = new Mongo.Collection('cards'); Cards.attachSchema(new SimpleSchema({ title: { type: Str ...

"Clicking on a jQuery div will cause it to slide down, and clicking again

I am currently working on this code snippet: $("#right").click(function() { $("#signin").stop().slideDown(300); }); At the moment, it drops down when clicked, but I would like it to slideUp() when clicked again. Your help is appreciated. On a relate ...

If an element with a "hidden" display property is loaded in the browser window, will it be visible?

Is an element in a hidden display still using memory when the page is loaded? It's convenient to have many elements on a page, but if 99 elements are hidden and only 1 is displayed, does that impact the loading of the page? I'm curious if the pa ...

Discover the size of objects in three.js

Looking for a way to determine the dimensions of a mesh in three.js? I have Collada (.dae) files and need to know their size in units (x,y,z). I've heard about using geometry.computeBoundingBox(), but could use more guidance on how to implement it. ...

Tips for creating a new route within a separate component in React JS without causing the previous one to unmount

I am currently developing a recipe website using React JS and React Router. On the HomePage, I have set up a display of cards, each representing a preview of a recipe. Each card is enclosed within a <Link></link> tag. When one of these cards ...

Tips for implementing a distinct texture on child elements through traversal techniques

My current dilemma involves working with an object that I've loaded using THREE.ObjectLoader(). This object consists of various elements, resembling a simple box made up of panels and sticks. My goal is to assign different textures to specific child ...

Utilize promise-style for Sequelize associations instead, please

I am in the process of merging information from 3 tables - Products, Suppliers, and Categories. My goal is to retrieve the row with SupplierID = 13. I recently came across a helpful explanation on How to implement many to many association in sequelize, whi ...

I am having trouble getting JQuery tablesorter to work, what am I missing?

As a newcomer to jQuery, I am attempting to implement Tablesorter, but unfortunately, it does not seem to be functioning properly on my table (the styling remains unaffected by the tablesorter css, and the sorting functionality is non-existent). Below is ...

What is the method to display the Vue.js component's identifier?

Here is a link to an official document: https://v2.vuejs.org/v2/guide/list.html#v-for-with-a-Component Below is a demonstration of a simple todo list: Vue.component('todo-item', { template: '\ <li>\ {{ titl ...

Comparing JSON Objects in Javascript

I'm in the process of developing a web application that retrieves data from a server and displays it to the user. The script pulls data from the server every 10 seconds, and if there's any change in the data, it alerts the user. Currently, the co ...

What is the significance of the expression $location.path() equal to '/a' in Angular?

I am currently delving into AngularJs for the first time and I have been studying the Angular documentation in order to grasp its concepts. While going through it, I came across this piece of code: $location.path() == '/a'. However, I am struggli ...

Passing information from a directive to a controller using AngularJS

I am looking to display the progress of a file upload using a file reader. Here is the HTML code snippet I have: <md-progress-linear id="file-progress-indicator" md-mode="determinate" value="{{progress}}"></md-progress-linear> and below is m ...

Is there a way to implement word wrapping in li text without making any changes to its width

Is there a method to wrap the content inside li elements without modifying their width? As an illustration, consider the following structure - <ul> <li>Text Example</li> <li>Text Example</li> <li>Text Exampl ...

Issue found: Passing a non-string value to the `ts.resolveTypeReferenceDirective` function

Encountering the following error: Module build failed (from ./node_modules/ts-loader/index.js): Error: Debug Failure. False expression: Non-string value passed to ts.resolveTypeReferenceDirective, likely by a wrapping package working with an outdated res ...