Using AngularJS: Verify the presence of a key/value pair in an object and create an array of objects

My current service method retrieves income data from different projects and creates a new array of objects. I am seeking assistance to sort this array by year and trimester in order to simplify looping through it using ng-repeat in the view.

The data structure I have returned by Incomes.buildAndGetIncomes() is as follows:


[
    {
        "projectName": "Deuxième test",
        "clientName": "Deuxième client",
        "typeIncome": "Accompte",
        "amount": 1000,
        "date": "2014-09-10",
        "notes": "Cheque / LDD",
        "trim": "third",
        "year": "2014"
    },
    // More income objects...
]

I want the desired data structure to be organized by year and trimester like this:


[  
    {
        year: 2014,
        trim: [
            {
                name : 'first',
                content : [
                    // some content
                ]
            },
            // More trimesters...
        ]
    },
    // More years...
]

Currently, I have a method called `trimestral` that attempts to achieve this sorting functionality. However, I am facing challenges with accurately checking for existing years and trimesters within the data structure.

This implementation checks if it's the first index of the loop and adds the year/trimester/content accordingly. While this works for that scenario, I struggle with handling cases where the year or trimester already exists. Any guidance on improving this logic would be greatly appreciated!

Answer №1

If you want to achieve this functionality without creating complex nested loops, you can consider utilizing underscore.js library. Using underscore.js, the code becomes more manageable and easier to follow. Here's an example of how you can accomplish this...

var quarterlyData = _(incomes).chain()
    .groupBy('year')
    .map(function (items, year) {
        return {
            year: year,
            quarters: _(items).chain()
                    .groupBy('quarter')
                    .map(function(content, quarter) {
                        return {
                            quarter: quarter,
                            data: content
                        };
                    })
                    .sortBy(function(i) { 
                        var quartersOrder = {
                            Q1: 1,
                            Q2: 2,
                            Q3: 3
                        };
                        return quartersOrder[i.quarter];
                    }).value()
        };
    })
    .sortBy('year')
    .value();

Check out the Live Demo here!

Answer №2

After seeking help from a skilled web developer friend, I learned the process can be simplified into two steps: constructing the object and then formatting it as needed.

Here's what I ended up doing:

self.trimestral = function(){
  var deferred = $q.defer();
  var global = [];
  self.buildAndGetIncomes().then(function(result) {
    var trimestral = {};

    var incomes = result;

    // First loop, construct the object
    angular.forEach(incomes, function(income, i){
      var year = income.year,
          trim = income.trim,
          newTrim = {},
          newTrimContent = {};          
          if(trimestral[year] === undefined){
            trimestral['year' , year] = {};

          }
          if(trimestral[year][trim] === undefined) {
            trimestral[year][trim] = [];               
          }
           trimestral[year][trim].push(income);        

    });

   deferred.resolve(global);

   // Second loop, format the data
    for ( prop in trimestral ) {          
      newYear = {};
      newYear.name = prop;
      newYear.content = [];          
      for ( trim in trimestral[prop]){
        newTrim = {};
        newTrim.name = trim;
        newTrim.content = [];           
        newTrim.content = trimestral[prop][trim];              
        newYear.content.push(newTrim);
      }
      global.push(newYear);
    }         
  });
return deferred.promise;    
};

Although there may be room for improvement, the solution works smoothly and is simpler than expected.

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

Integrating individual front end JavaScript files into an Express.js application

I've been working on a JavaScript file that contains over 200 lines of code for the front end logic of my project. It handles interactions like button clicks, image displays, and more, similar to a game. However, I'm struggling to figure out how ...

Encountering a 400 error when utilizing the Google Translate free API with Curl

I am attempting to utilize the free Google Translate API that is derived from Firefox's S3 Google Translator addon, by incorporating the following code: https://translate.google.com/translate_a/single?client=t&sl=auto& tl=en&hl=en&dt= ...

Attempting to create a fresh string by substituting certain characters with different ones

Exploring TypeScript, I encountered a puzzle where I needed to substitute specific characters in a string with other characters. Essentially, replacing A with T, T with A, C with G, and G with C. The initial code snippet provided to me looked like this: e ...

JkMegaMenu drop-down menus in Chrome are shifting to the left when the window is resized

Currently, I am utilizing the JKmegamenu plugin to incorporate a megamenu into a website that is currently under development. The megamenu functions properly and has an attractive appearance. However, there seems to be an issue where the drop-down divs shi ...

Change the text of a button by using an input field

How can I dynamically update button text based on input field value? <input class="paymentinput w-input" type="tel" placeholder="0" id="amount-field"> <button id="rzp-button1" class="paynowbutton w-button">Pay Now</button> I want the bu ...

Modifying SVG gradients with JavaScript

My goal is to modify the stop color of a gradient displayed in an SVG element. I've been trying to adjust the stop-color attribute, but so far, my attempts have not been successful: <svg><defs> <linearGradient gradientTransform="rotat ...

All about the ins and outs of JavaScript and manipulating the

I attempted to use DOM manipulation to change the page background color and div background color by utilizing getElementById and onclick methods. I wrote some code to make this happen, but unfortunately, it did not work as expected. Can anyone identify wh ...

Avoid executing top-level path middleware with app.use('/') in Express routing when directly accessing child or nested paths

Perhaps the title may not be as accurate as I hoped, but I have a very simple example to share. On my website, there are two main areas - a public area and a restricted admin area. example.com/admin (admin home page) example.com/admin/news (news page) ...

The Vuex commit has exceeded the maximum calstack size limit

Currently facing an issue https://i.sstatic.net/l1WWH.png The error seems to be isolated to this specific section mounted() { this.$nextTick(() => { let ctx = this.$refs.canvas.getContext('2d') let { chartType, dataOptions ...

When using sequential jQuery 'pages', an error referencing the third frame occurs

I am new to using javascript/jquery and have been experimenting with the w3schools tutorials and jquery documentation. I created a page where user input is accepted and javascript prints output based on that input. I tried modifying it to work sequentially ...

Utilizing ECMA 5 code within Internet Explorer 8

My current code includes the use of hls.js for playing hls streams. The original code is in ECMA version 6 and then transpiled into ECMA 5, which can be found in the dist folder (link provided). It works fine in most cases. However, when trying to render ...

Angular ng-click function is not functioning properly within a DataTables table row

I am facing an issue in my application where I am using both Angular and jquery.dataTables. The problem arises when I try to incorporate the ng-click angular directive within the dynamically created table using datatables, as the ng-click event does not se ...

Analyzing: Sending the uploaded file or image to the backend server

Included in my HTML is a form: form.html: <form method="post" enctype="multipart/form-data" action="/"> <input type="file" name="pic" id="imgupload"> </form> <script> document.getElementById("imgupload").onclick = function ...

What is preventing the Date Picker in Selenium from accepting attribute values when using JavaScript for the Spice Jet application?

My implementation of JavaScript for the Date picker in Selenium is running successfully, however, the date is not being selected in the date picker. public class SpicJetBooking { static WebDriver driver; public static void main(String[] args) t ...

What is the best way to verify the font-family using JavaScript?

To verify if the user has installed the font family, we can run a JavaScript function with AngularJS. I am using a Typekit font and have only loaded this service for users who do not have this specific font. ...

Javascript window.scroll function malfunctioning in some browsers while running in localhost

Check out this codepen link where everything is working, but unfortunately not locally: https://codepen.io/LoudDesignStudios/pen/RwxPJKY <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> < ...

Leveraging the NextAuth hooks, employ the useSession() function within the getServerSideProps

I'm currently working on retrieving data from my server based on the user who is logged in. I am utilizing Next-Auth and usually, I can easily obtain the session by calling: const { data: session } = useSession(); In a functional component, this work ...

Determine the size of the JSON string

I am working with the following JSON string: var j = { "name": "John" }; alert(j.length); When I run this code, it alerts 'undefined'. How can I find the length of a JSON array object? Thank you. ...

Tracking progress with a dynamic bar from the beginning to the end

Struggling to develop a progress bar that corresponds to project start dates and end dates. With a roster of 100 projects, spanning from mid-2017 to future launches, the aim is to depict progress between 2018, 2019, and 2020. If a project commenced in 2017 ...

output the elements of a list of strings

I am trying to generate a list of strings based on a given list of numbers. For example, let's say the input list is list=[1,2,5,25,6] Expected output: ['Odd', 'Even', 'Odd, multiples of 5 and odd', 'multiples o ...