Manipulating object information within a nested for loop

I have a variable called jobs in my scope, which is an array containing objects for each department along with their respective data.

[ “Accounting”: { “foo” : “foo”, “bar” : "bar" }, “Delivery”: { “foo”: “foo”, “bar”: “bar” } ]

When using the HTML5 date input, dates must be converted to new Date() format in JavaScript, even if they are already in the correct yyyy-mm-dd format. Instead of manually converting each date, I wanted to use nested loops to do it efficiently since there are multiple dates to convert.

angular.forEach($scope.job, function(value, key){
    angular.forEach(value, function(innerValue, innerKey){
       if(typeof(innerValue) === "string"){
         var matches = innerValue.match(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/);
         if(matches){
               // Need to set $scope.job.key.innerKey = new Date($scope.job.key.innerKey) here 
         }
       }
     });
  });

The challenge I’m facing is figuring out how to access and edit the current item being looped over within the $scope.job object at specific key and innerKey values. I couldn’t find much documentation on using ‘this’ in such scenarios.

Answer №1

To access variable property names, simply use the object notation with square brackets like this: []

if(matches){
   value[innerKey] = new Date(value[innerKey]);
}

Answer №2

An alternative method could involve creating a custom directive that encapsulates the date input, with the conversion logic handled within the directive's controller. By implementing this approach, the framework automates the iteration process for every occurrence of the specialized date input directive.

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

The div element is finally loading properly after multiple clicks

I need some assistance with loading dynamic divs in Angular. I have created a button that adds new divs each time it is clicked in a specific area. However, once these new divs are added, they appear incorrectly: After adding the divs, the editor and cale ...

Using Angular to create a web API routing system with the PUT method

I'm encountering an issue with routing in my Angular app + web API. I am trying to update a user, but it's not working as expected. The server is returning a 404 error: Failed to load resource: the server responded with a status of 404 (Not Fo ...

The $.post method is malfunctioning

I am experiencing an issue where the onchange function is working properly, but the $.post function is not functioning as expected. Below is the HTML code snippet: <input type="checkbox" id="chk" value="3" onchange="checkradio(this.value)"/> Here ...

Update the var value based on the specific image being switched using jQuery

I have implemented a jQuery function that successfully swaps images when clicked. However, I am now looking to enhance this functionality by passing a parameter using $.get depending on the image being displayed. Here is the scenario: I have multiple comm ...

Connect parent-child models with checkboxes

Currently, I am working on an application where I need to link checkboxes for a role-based menu. The challenge lies in dealing with parent-child checkboxes and ensuring that the values of checkboxes are checked based on user input 1, 1.1, 1.2, 2, 3. Despi ...

Issue encountered during JSON file extraction (Firefox)

Within my Node.js application, there is a specific route where I retrieve a large JSON dataset from a Postgres database and then send it in a compressed format as a response. To accomplish this, I utilize the Zlib module to compress the data using gzip. Be ...

Placing a hyperlink within template strings

Currently, I am working on implementing a stylish email template for when a user registers with their email. To achieve this, I am utilizing Express and Node Mailer. Initially, my code appeared as follows: "Hello, " + user.username + ",&bs ...

Load the flexslider once the fancybox container is opened

In my experience, I have found flexslider and fancybox to be very useful plugins. Individually, they work perfectly fine on a website that I am currently working on. However, when I tried placing a flexslider gallery inside a fancybox div, I encountered a ...

JavaScript's static function and variable that operate asynchronously

Encountering an issue with a "static" function in JavaScript (Node.js server). User.create = function(data, _callback){ var node = db.createNode(data); var _user = new User(node); _user.save(function(err){ if(err) return callba ...

Effortless JavaScript function for retrieving the chosen value from a dropdown menu/select element

I've figured out how to retrieve the value or text of a selected item in a dropdown menu: document.getElementById('selNames').options[document.getElementById('selNames').selectedIndex].value In order to simplify this code, I&apos ...

Picture is currently not showing up in the division

I am currently working on an HTML page and I'm having trouble displaying an image within a division element. Below is the code snippet I tried: Here is the JavaScript part of my code: var filename = "andrew.png"; $("#detected").show().html('< ...

Discovering the oldest date in an array with JavaScript

Is there a way to identify the earliest date, also known as the minimum date, within an array using JavaScript? For instance: ["10-Jan-2013", "12-Dec-2013", "1-Sep-2013", "15-Sep-2013"] The desired output would be: ...

Alter the position of the anchor object in the center using JavaScript upon page load

When my page loads, I want to automatically jump to a specific anchor tag and ensure that the anchor object is centered in the window. Initially, I implemented a basic function to achieve this: <body onload="goToAnchor();"> <script type="text/ja ...

Angular: Dividing a web page with its individual controller

Exploring Angular Composition An individual new to Angular is eager to implement the concept of "composition," where the main page contains various smaller web page components. Starting off with just a Header section that binds perfectly. <html> &l ...

Managing active dropdown menus in VueJS

I'm having trouble figuring out why my navigation menu and method to open subitems on click are not working correctly. [![dropdown_menu][1]][1] new Vue({ el: '#app', data: { //menu "menu_title": "a", "child_ro ...

locate the presence of a specific letter within a sequence of characters

When dealing with strings like "Galley1", "Galley2", "Galley3", "Galley4", "Galley5", "Galley6" up to "Galley20", the task is to determine whether a condition is met. The condition should return true if the string contains a number between 1 and 7, inclusi ...

How to Retrieve Component HTML Output in Vue beyond the Template Tag?

Is there a way to access the output of a component, such as ComponentName, outside of the template in Vue.js? For example, in data, methods, or during the mounted lifecycle hook. For instance, consider a Vue file named components/Test.vue: <template&g ...

Verify user access rights with Vue.js Laravel

I am currently working on a project using Laravel and Vue. In my middleware, I have the following code: public function handle($request, Closure $next) { $user = auth()->user(); if ($user->hasRole('admin')) { return ...

My app's custom barrel configurations don't appear to be functioning properly in Angular 2's system-config.ts

My system-config.ts file is as follows: 'use strict'; // SystemJS configuration file, for more information visit: // https://github.com/systemjs/systemjs // https://github.com/systemjs/systemjs/blob/master/docs/config-api.md /***************** ...

store the id of each li element dynamically into an array

In my code, a list is generated dynamically and each list item has a special id. I am trying to store each li "id" in one array. This is JavaScript code: var i = 0; $("ul#portfolio li").each(function(eval) { var idd = new Array(); idd[i] = $(this ...