Include features once JSON has been loaded

Received this JSON data:

{
"info": [
{
  "id": 999,
  "products": [
    {
      "id": 1,

    },
    {
      "id": 2,
    }
  ]
}
]
}

Info
-- products
-----id

Here is the factory code snippet:

AppAngular.factory('model', ['$http', function ($http) {
    return {
        load: function (scope) {
            $http.get('mydomain/api').success(function (data) {
                    var myObject = {};
                    angular.extend(myObject,data);
                    for (var i = 0; i < myObject.info.length; i++) {
                        for (var j = 0; j < myObject.info[i].products.length; j++) {
                        myObject.info[i].products[j].selected = true;
                        myObject.info[i].products[j].quantity = 1;
                        .....
                       }
                    }
                }
            });
        }
    };
} ]);

Is there a more efficient way to add properties after using angular.extend, without multiple for loops? We want to enhance the object with additional properties post JSON retrieval. For example:

Info
    -- products
    -----id
    -----selected
    -----quantity

Answer №1

If you're looking to iterate through objects in Angular, consider using angular.forEach:

AppAngular.factory('model', ['$http', function ($http) {
    return {
        load: function (scope) {
            $http.get('mydomain/api').success(function (data) {
                var myData = {};
                angular.extend(myData,data);
                angular.forEach(myData.info, function (info) {
                    angular.forEach(info.products, function (product) {
                        product.selected = true;
                        product.quantity = 1;
                        .....
                    });
                });
            });
        }
    };
}]);

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

Develop a TypeScript class that includes only a single calculated attribute

Is it advisable to create a class solely for one computed property as a key in order to manage the JSON response? I am faced with an issue where I need to create a blog post. There are 3 variations to choose from: A) Blog Post EN B) Blog Post GER C) Bl ...

My GraphQL Query is throwing a JSON error when using the curlopt_postfields option. What could be causing this

I'm having some trouble with my code and I keep encountering this error message. Any ideas on what might be causing this issue? I believe I've properly escaped all the quotations. {"errors":[{"message":"json body could ...

Utilizing sorting and filtering functionalities on an Angular data table

Recently, I decided to enhance my Angular skills by downloading a demo app. While working on the basics of Angular, I encountered a problem with applying a filter and sort to a data table in the app. Despite referring to some examples, I am uncertain about ...

Having trouble understanding expressions in the AngularJS tutorial

When attempting to follow the AngularJS tutorial for version 1.2.12, I noticed that my localhost displays a different result compared to the live demo shown in the tutorial. Specifically, when following the initial steps, my expressions are not being rende ...

Manipulating state in React

Currently, I am enrolled in Samer Buna's Lynda course titled "Full-Stack JavaScript Development: MongoDB, Node and React." While working on the "Naming Contests" application, I encountered a piece of code within the App component that has left me puzz ...

Setting up an i18n project in AngularJS

I just embarked on my angularjs journey yesterday with little to no prior knowledge about it. My initial goal is to centralize all the labels for my UI in a file (to facilitate swapping them out for i18n). As far as I know, this can be achieved by importi ...

Initializing functions and variables with ng-init

What is the correct way to implement the code below in angular js? I need to run the loader method and set some variables as well. ng-init="loader()" data-ng-init="firstName='John'" ...

AngularJS - Embedding input fields within an HTML table within a form

The system is up and running now... I retrieve an array and insert the name into an input field. Then, I update the name in the input field and send it back using a PUT request. Here is the HTML code: <form id="edit"> <table> < ...

Runner for Jasmine specifications and directive template URL

Within my angular application, I have encountered two distinct methods for running unit tests. The initial approach involves running Karma, while the alternative is to utilize the Jasmine spec runner due to its simplicity in terms of debugging. One chall ...

The Jetty server is unable to provide JSON responses

Whenever I use the command mvn jetty:run to initiate my server, it fails to return JSON formatted strings. Instead, it only returns raw strings or "null" for all objects. In the code snippet for my endpoint, you can observe that there is no variation in r ...

Exploring the process of iterating through a JSON post response and displaying its contents within image divs

After receiving a JSON post response, I am looking to iterate over the data and display it in image divs. Can anyone provide guidance on how to achieve this using JavaScript? Thank you. Here is the JavaScript code that handles the JSON post response: cor ...

Utilizing AngularJS to extract data from a query string

After making significant progress on my previous project, I found myself in need of some final assistance that has proven to be elusive. I successfully built my entire angular controller and populated all the necessary data for the page. However, I am str ...

Using PHP to insert nested JSON data into a MySQL database table

I'm currently facing a challenge where I need to insert nested JSON data obtained from the tmdb API into my mySQL database. Although I already know how to insert nested JSON, the new record I'm working with involves a many-to-many relationship b ...

Refreshin the attached DOM to a directive without a page reload

Within a directive, I have implemented some text and a video tag in the code below: app.directive('ngAzuremediaplayer', function () { return { restrict: 'AE', priority: 10, link: function (scope, elem, attr ...

Is it possible to integrate external search functionality with Vuetify's data table component

I am currently working with the v-data-table component from Vuetify, which comes with a default filter bar in its properties. This table retrieves data from a local JSON file. The issue arises when I have another external component, a search bar created u ...

Tips for updating a column with just one button in AngularJS

On my list page, there is an Unapproved button. I am new to angular and struggling to figure out how to retrieve the post's id and update the corresponding column in the database to mark it as approved. Can someone provide guidance on how to accomplis ...

Obtain the dimensions of the outermost layer in a JSON file

Could you please help me extract the dimensions (600*600) of the outermost layer from the JSON below dynamically? { "path" : "shape\/", "info" : { "author" : "" }, "name" : "shape", "layers" : [ { ...

Extracting data from a JSON object

Currently, I am facing an issue with my node.js code that is not working as expected when trying to fetch a value from a 3rd party website in JSON format. Although my code works fine for similar cases, it is failing to extract the price of a specific item ...

Manually validate inputs in Angular

I've encountered an issue with the bootstrap datepicker. When I click on the date icon, the date and time automatically populate in the input field. However, the input remains invalid because I haven't directly interacted with it. Has anyone else ...

Enrich PowerShell with additional JSON functionality

Does anyone know of a straightforward method to merge one JSON file with another and save the combined output to a new file using PowerShell? I am currently working on creating a recursive function for this purpose, but I'm wondering if there is a sim ...