Creating Nested Arrays in Angular: A Step-by-Step Guide

I'm struggling to create an array of arrays and I can't seem to figure out what's wrong with my code. Here's the snippet:

c.push({ id: data[0]["id"], title:data[v]["title"], date:data[v]["date"],
         text:data[v]["text"], favorit:"true" });
$scope.favoriti.push({c});
c=[];

When I try to access the array using alert, it shows undefined.

alert( $scope.favoriti[0]["id"] );

Can someone please guide me on how to create this kind of array so that I can access it with the alert function? Your help is greatly appreciated! Thank you.

Answer №1

please review this piece of code.

var app = angular.module('app', []);
app.controller('controller', function($scope) {
  var data = [{
    id: 1,
    title: 't',
    date: 'd',
    text: 'text'
  }];
  
  var c = [];
  $scope.favoriti = [];
  var v = 0;

  c.push({
    id: data[0]["id"],
    title: data[v]["title"],
    date: data[v]["date"],
    text: data[v]["text"],
    favorit: "true"
  });

  $scope.favoriti.push(c);
  console.log($scope.favoriti);
  console.log("id : " + $scope.favoriti[0][0]["id"]);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
</div>

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

Comparing Methods for Populating Dropdown Lists: JavaScript vs Database Integration (Mysql-Django)

I am looking to implement 3 dropdown lists that work together: Select: State Select City Select: Restaurant When a selection is made in the first dropdown list, it should update the "Select City" list. Similarly, when a selection is made in the sec ...

Display issue with React TypeScript select field

I am working with a useState hook that contains an array of strings representing currency symbols such as "USD", "EUR", etc. const [symbols, setSymbols] = useState<string[]>() My goal is to display these currency symbols in a select field. Currently ...

Is it safe to securely store connection credentials in javascript?

I am currently working on developing a web chat app for a Minecraft server using this API. However, the demo script I am referring to displays connection information in plain text, making it easily visible to any client's computer. Is there a way to s ...

Middleware in Express.js Router

In the following code snippet, I am aiming to limit access to the 'restrictedRoutes' routes without proper authorization. However, I am encountering an issue where 'restrictedRoutes' is affecting all routes except for those within the & ...

Ag-Grid is failing to display MenuTabs

I need help getting the columns menu tab to appear in Ag-Grid. It should be a simple task. I want both the general and columns tabs, similar to the demo shown here Currently, when I specify both tabs, only the general tab is displayed. If I specify just t ...

Use jQuery to switch back and forth between the login and registration forms on a single

I've set up two forms, one for login and one for registration, with the default view showing the login form. There's a link that says "Don't have an account?" and when it's clicked, the registration form will display while the login for ...

I am experiencing issues with the POST method in my RESTAPI in node.js and it is not functioning

I recently started learning Node.js and Express.js with the goal of creating a basic API to retrieve data from a JSON file using the GET method, and to add a new user using the POST method. The GET method is functioning correctly, but I have encountered a ...

What is the best way to assign default values when destructuring interfaces within interfaces in TypeScript?

My goal here is to create a function that can be used with or without arguments. If arguments are provided, it should work with those values; if not, default values should be used. The issue I'm facing is that although there are no TypeScript errors ...

Is it possible to set a value for a jQuery function after the function has

I am a beginner when it comes to javascript & jQuery. I am currently working on a feature for my website where users can display badges they have earned on their own site by simply copying and pasting a bit of code that I provide. Although the javascript p ...

Navigate through all child views recursively in Swift to locate a particular class and add it to a collection

Struggling to crack the code on this one. A similar inquiry was made at this link: Swift: Get all subviews of a specific type and add to an array Although the previous solution worked, I've come to realize that there are numerous nested subviews to c ...

Issues with Jquery's Slice, Substr, and Substring functionalities

I need to divide the content into two separate sections. Unfortunately, the first variable (summarySentence1and2) is not capturing the desired result, while the second variable (summarySentanceOthers) is storing all of the content from the source field. I ...

How can I position an object in Three.js to perfectly align with the left half of the screen, adjusting both its width

When the button is clicked, I want the entire 3D object's width and height to adjust to fit on the left side of the screen, while displaying a div HTML info on the right side. How can I make the object fit on the left side of the screen? Can I use ma ...

Every time I click the login button, my HTML page opens again in a new tab. How can I resolve this problem?

Every time I click the login button, my HTML page opens again in a new tab. How can I resolve this issue? I've created an HTML form where clicking on login opens the HTML page in a new tab. What is the solution to fix this problem? My HTML Code <!D ...

Attempting to set up an Ajax webform with various outputs, but encountering issues with functionality

I'm encountering an issue while creating interactive exercises. I want to display correct or incorrect answers immediately after submission using JSON for retrieving responses, as suggested in a forum. However, my AJAX code isn't working at all. ...

Reduce and combine JavaScript files without the need for Grunt or Gulp

My project involves working with over 50 JavaScript files that I want to combine and compress to optimize file size and minimize HTTP requests. The catch is, the client's system does not have Node or npm installed. How can I accomplish this task witho ...

adjusting state with React hooks

I currently have two buttons labeled create signature and Create delivery in my file PageOne.tsx. When the state is set to InDelivery, both buttons are visible. My goal is to hide the Create delivery button initially. However, when a user clicks on the cr ...

When launching a modal window, the ability to dynamically change the URL and seamlessly transition into the modal window using Bootstrap 3, AngularJS, and CSS

Hello, I am looking to create a modal window that changes the URL when opened. For example, if a user clicks on a link with #123, the URL should change to include #123 after the slash. Additionally, I would like to be able to send someone a link directly ...

What is the quickest method to perform a comprehensive comparison of arrays and combine distinct objects?

I am currently working with NextJS and Zustand and I have a state in Zustand that consists of an array of objects: [{a:1, b:2}, {a:2, b:3}] Additionally, there is another incoming array of objects that contains some of the existing objects as well as new ...

Issues with the Comments Adapter arise when CKEditorError errors are not properly handled after a Promise rejection

Regarding the error codes, are they automatically integrated into the editor or do I need to handle them manually? For example, if a server error occurs when adding a comment using CommentAdapter and the promise is rejected, will the CKEditor UI display th ...

Exploring Time Scaling and Range Adjustment in D3 Barcharts

My dataset looks something like this: dateRange = [ { date: 2017-03-23, value: 10 }, { date: 2017-03-25, value: 15 }, { date: 2017-04-01, value: 13 }, { date: 2017-04-02, value: 19 } ]; The results can be viewed here: https://embed.plnkr.co/iOBAuCZmo ...