How to loop through a JSON object using AngularJS

I have a nested JSON "object" called znanja that I want to showcase on my webpage. It contains various values that I wish to present in the form of a list. Essentially, I am constructing a portfolio to highlight my skills. So far, I've been able to display the title and the entire object, as well as call specific attributes (e.g., {{ znanje.ena }}). However, I am struggling to figure out how to display them without including the attribute name. When I simply use {{ znanje }}, the values are listed as they appear in the JSON structure. Is there an angularJS directive that can help me achieve this? Being new to Angular, I would greatly appreciate any guidance.

Here is my view:

<div ng-repeat="skill in skills">
    <h1>{{ skill.title }}</h1>
    <p ng-repeat="znanje in skill.znanja">{{ znanje }}</p>
</div>

And here is my data:

  $scope.skills = [
  {
      title: "Code Knowledge",
      znanja : [{
        ena : "Javascript",
        dva : "HTML",
        tri : "CSS",
        stiri : "SASS"
      }]
  },
  {
      title: "Base Code Knowledge",
      znanja : [{
        ena : "Java",
        dva : "PhP"
      }]
  },
  {
      title: "Data",
      znanja : [{
        ena : "MongoDB",
        dva : "MySQL"
      }]
  }
  ];

PS: I opted to name the znanja attributes ena, dva, tri, stiri instead of 1, 2, 3, 4 for easy referencing in the HTML.

Answer №1

In order to apply a loop to the desired object, you must target the 1st element within the znanja array specifically by using skill.znanja[0], rather than just skill.znanja.

By making this adjustment, your ng-repeat will be able to iterate through each property of the object and display the values for each property of znanja[0] with {{znanje}}.

Markup

<div ng-repeat="skill in skills">
    <h1>{{ skill.title }}</h1>
    <p ng-repeat="znanje in skill.znanja[0]">{{ znanje }}</p>
</div>

Answer №3

If you wish to showcase the attributes, a DOM element needs a particular value to render, requiring manual attribute calls for at least one iteration. However, in cases of repetition, you can utilize ng-repeat as you have demonstrated.

For displaying each attribute on the HTML page individually and later accessing them, or simply showing as a JSON object, passing the JSON will suffice.

Answer №4

<div ng-repeat="ability in abilities">
    <h1>{{ ability.name }}</h1>
    <p ng-repeat="(key, info) in ability.knowledge[0]">{{ key }} <b>:</b> {{ info }}</p>
</div>

Answer №5

(tested) merged both znanja-object 0 and its corresponding value:

    <div ng-repeat="skill in skills">
    <h1>{{ skill.title }}</h1>

    <p ng-repeat="(key, val) in skill.knowledge[0] ">{{ val }}</p>
    </div>

Answer №6

To tackle this problem effectively, consider adjusting your data structure. Instead of wrapping the lists in an array, transform them into an object map:

$scope.skills = [
{
  title: "Code Knowledge",
  knowledge : {
    one : "Javascript",
    two : "HTML",
    three : "CSS",
    four : "SASS"
  }
},
{
  title: "Base Code Knowledge",
  knowledge : {
    one : "Java",
    two : "PhP"
  }
},
{
  title: "Data",
  knowledge : {
    one : "MongoDB",
    two : "MySQL"
  }
}
];

If the array serves no purpose (given that all examples consist of only one element), it can be removed.

By making this adjustment, there is no need to modify the original ng-repeat code.

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 the differences: MEAN stack angular routing versus express routing

After incorporating Angular into my express generated projects, I have discovered a love for it. Recently, I integrated angular routing into one of my test projects and now I'm curious about the pros and cons of using angular routing compared to pure ...

Establish a connection between two pre-existing tables by utilizing the Sequelize framework

I have two tables already set up (User and PaymentPlan), but they were not initially linked together. PaymentPlan.ts import { DataTypes, Model } from "sequelize"; import { sequelize } from "./DBConnections/SequelizeNewConnection"; exp ...

Organizing Checkbox Groups for Validation in Bootstrap

My form consists of 2 checkboxes and I only want to submit the form if at least one checkbox is checked. The current code requires both checkboxes to be checked, but I tried grouping them using the name attribute without success. How can I group checkbox ...

Is AngularJS more than just a view?

My goal is to create a single-page Angular app with a central view surrounded by tools like a sidebar and top bar. However, when the user is not logged in, I want the view to display a login partial while hiding the side- and top bars. Additionally, ther ...

Centering div elements in ASP.NET using C# and Javascript

I am looking to create a functionality on my website where, upon clicking a button, a DIV appears in the center of the browser, overlaying all other content. I have already created the DIV and managed its visibility toggle, but I'm struggling with the ...

Issue with Bootstrap Modal Tabs not functioning

CODE <!-- Unique Modal Code --> <div class="modal fade" id="uniqueModal" tabindex="-1" role="dialog" aria-labelledby="uniqueLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="m ...

Retrieve all elements from an array that have the highest frequency of occurrence

Consider an array like [1,4,3,1,6,5,1,4,4]. The element with the highest frequency in this array is 3. The goal is to select all elements from the array that have a frequency of 3, so in this case we would select [1,4]. To achieve this, one possible meth ...

Encountering a shell script parsing problem: Could it be an unexpected INVALID_CHARACTER due to Windows cmd shell quoting

What am I trying to accomplish? I have a JSON file called sonar-report.json. My goal is to iterate through the sonar-report.json in a shell script and extract values from the JSON file. To achieve this, I am using jq as my JSON parser (). Below is the cod ...

Tips for linking attributes to a function using ES6

I'm currently working on a stateless React.js component called myView. I want to keep the syntax as concise as possible, but I'm having trouble understanding how to export a default function with an attached object. Here's an example in myVi ...

How can you access a function from within another function in the same object while keeping the object structure largely intact?

Seeking a solution using JavaScript (ES6), I am in need of referencing a handler function called onKeyup. This will allow me to both add and remove an event listener in two functions that are declared within the same object. Can you suggest how I can acce ...

When using VueJS checkboxes with object values bound to an array property, they do not get removed from the array when unchecked

Within my VueJS component, I am utilizing an array named selectedJobs to track checked checkboxes in an HTML table. The data displayed in the table is sourced from an array of objects called replenJobsList. /* My Component */ <template> ...

Steps to make a unique custom tooltip without using jQuery by utilizing the title attribute

I am looking to implement a tooltip similar to the one shown in the image. The value in the title tag is dynamic and fetched from the controller. https://i.sstatic.net/C2v3D.png Below is the code snippet of my table: <table border="1" cellpadding="10" ...

Exploring the MEAN stack: Is there a distinction between housing my views in the public directory versus the server views directory?

During frontend development, I typically store all my HTML files in the public/app/views directory. However, I've observed that some developers also have a separate views folder for server-side files, such as .ejs files. Is this practice primarily to ...

Utilize formData to upload an image to a database with ajax

I'm struggling with the process of uploading an image from an HTML form. The goal is for the form to send the image name along with other data to a PHP page. Instead of using the serialized function, I opted for formData as it has the capability to h ...

Monitoring page reload with JavaScript

Here is an example of tabbed content: <div class="tab"> <button class="tablinks" onclick="openCity(event, 'NewYork')" id="defaultOpen">New York</button> <button class="tablinks" onclick="openCity(event, 'LosAngeles& ...

JavaScript data manipulation: Determining percentage change within a nested JSON structure

Provided is a JSON file structured like this... data =[ { key: "london", values: [ {day: "2020-01-01", city: "london", value: 10}, {day: "2020-01-02", city: "london", value: 20}, {day: " ...

Updating choices within a div in real-time by changing the select box dynamically

I need to swap out the select box that is nested inside this div <div class="models"> <select disabled="disable"> <option>Model Name</option> </select> </div> I am attempting to target the div and manipul ...

Jquery - Ajax: Error in Syntax JSON.parse: character not expected

I am encountering an issue with parsing JSON data on my client-side code. The JSON received from the server looks like this: [{"name":"Bubble Witch Saga 2","impressions":10749},{"name":"Grinder","impressions":11284},{"name":"Loovoo","impressions":12336},{" ...

Is there a way for me to retrieve the variable from one function and use it in another

I have a tool for managing images with descriptions that allows me to edit both the text and the image file. The challenge is saving the modifications I make in my database. Currently, I can only save changes if I modify the image itself. If I only update ...

Adding new information to MongoDB

Greetings to all the wonderful users of stackoverflow! I'm currently encountering an issue involving mongodb and nodejs. I'm attempting to insert data into mongodb, and I have all the necessary data ready in a csv file format. Here's a brie ...