Angular array: showcasing both keys and values in an efficient manner

Within my Angular code, I have created an array structured like this:

$scope.test = [
  {'item1' : 'answer1' },
  {'item2' : 'answer2' },
  {'item3' : 'answer3' }
];

This array contains various items and corresponding answers.

My goal is twofold. Firstly, I aim to utilize ng-repeat to iterate through the array and display the keys along with their respective values separately. My initial attempt was as follows:

<div ng-repeat="(key,value) in test">
  {{key}} = {{value}}
</div>

However, the output generated by this code does not align with what I desire. Instead of displaying as intended:

item1  = answer1
item2  = answer2
item3  = answer3

The ng-repeat currently outputs:

0  = {"item1" : "answer1"}
1  = {"item2" : "answer2"}
2  = {"item3" : "answer3"}

Secondly, I wish to use the keys from this array to fetch data from another array. For instance, consider this setup:

$scope.item1.question = 'some question';

As I traverse through the ng-repeat loop, I hope to achieve something like:

{{key}}.question

In order to display the phrase 'some question'. Unfortunately, this particular approach does not yield the desired outcome.

Answer №1

1. Utilize nested loops with ng-repeat.

When using ng-repeat, it only iterates over the array and displays the index of each element. To display the objects contained within, you need to nest another ng-repeat loop:

<div ng-repeat="obj in test">
    <div ng-repeat="(key,value) in obj">
        {{key}} = {{value}}
    </div>
</div>

2. Access objects using square brackets and wrap them inside an object.

If you want to access an object with a dynamic key from another object within an ng-repeat, use square bracket notation to allow expressions:

$scope.model = {
    item1: {
        question : 'some question 1'
    },
    item2: {
        question : 'some question 2'
    },
    item3: {
        question : 'some question 3'
    }
}; 

HTML:

<div>
    {{ model[key].question }}
</div>

Check out this interactive example on JSFiddle!

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

Is it possible for a React blog to be included in search engine results?

As I work on building my blog using React, Node.js, Express, Sequelize, and other technologies, a question has arisen in my mind: Will search engines index my articles, or will only the homepage of my site be noticed? For instance, if I have an article ti ...

The compatibility issue between Angular JS variables and Twig is causing functionality problems

issue Currently, I am utilizing a combination of Twig templates and AngularJS. controller public function testAction() { $this->render('AcmeDemoBundle:abc.html.twig'); } javascript code var scotchApp = angular.module('scotch ...

What steps should I take to export a function from a React functional component in order to create a reusable library?

Currently, I am in the midst of developing a React component library and one of my components contains a function that I want to export. The purpose of the addParticle function is to enable users of the library to dynamically insert particles into a cont ...

Make sure to place the <i> tag within the <li> tag at the bottom to automatically resize the height

Currently, I am working on customizing my menu design with a mix of bootstrap and font awesome CSS styles. It would be easier to demonstrate the issue on a live page. My main objectives are two-fold: I want to position the chevron icon at the bottom with ...

Issue with AngularJS: Not able to get second app in template to function properly

I have encountered a puzzling issue with my two nearly identical apps. The first one seems to be running smoothly as expected, while the second one doesn't appear to be executing at all. Here is my code (jsfiddle): <div ng-app="passwdtool" ng-con ...

Various Plus/Minus Containers

One issue I am facing is that when using multiple counters on the same page, my - and + buttons to decrease or increase the number in a text box do not function properly. The script provided below works for one counter. How can I modify this code so that ...

Having trouble understanding how to receive a response from an AJAX request

Here is the code that I am having an issue with: render() { var urlstr : string = 'http://localhost:8081/dashboard2/sustain-master/resources/data/search_energy_performance_by_region.php'; urlstr = urlstr + "?division=sdsdfdsf"; urlst ...

Remove any replicated elements from a list field within an array of objects within every document

{ "_id" : "1235", "details" : [ { "productId" : "PRODUCT1", "myList" : [ "0001", "00 ...

Issue: React error message indicates that the .map() function is not recognized. The API response is in the form of an object, making

As a newcomer to REACT.JS, I am currently facing the challenge of extracting data from an API for my project. Utilizing "Axios" for sending the get request, I have encountered a situation where the response comes back as an array in one API and as an objec ...

When transferring JSON to JavaScript within Laravel, the JSON data gets converted into HTML entities by JavaScript

Within my Laravel controller, I am constructing a multidimensional associative array: $trendChart = Array ( "series" => Array ( "data" => Array(1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5) ), "xaxis" => Arr ...

Tips for retrieving data from Angular dropdown menus

How to Retrieve Selected Value in AngularJS HTML <select data-ng-model='datasource.value' ng-options='sourcetype.dataSourceType as sourcetype.dataSourceDesc for sourcetype in sourcetypes' data-ng-change="getAccountCredentials(sourc ...

ever-evolving background-image with dynamic CSS styling

Being new to both PHP and Javascript, please excuse any mistakes in my explanation. I have information stored in a PHP array that I bring to my index page using the function below (located in a separate file called articles.php that is included in my index ...

Can jQuery be utilized on a freshly opened window?

Solving this challenge has become quite the task, as I've been at it for hours now. Maybe one of you can provide some assistance. My current code appears like this: $('.clickable').on('click', function() { var id = $(this).at ...

Seamless connection through Shopify's Buy Button

I have created a website using HTML5 and I am trying to create a direct link to a product checkout page without all the additional formatting and features that come with BuyButton. Shopify has provided me with this code: <div id='product-component ...

Display or conceal toggle in ruby utilizing jQuery

I'm facing an issue with the functionality of a button that is meant to hide and show a form using jQuery. Initially, I added a path to signup in the button so that it would display and function properly. Now, when I click the button, the form shows u ...

Ensuring that Bootstrap rows fill the entire height within a column

While working on a template with nested rows and columns in Bootstrap, the layout ended up looking like this: <div class="col-5"> <div class="row"> <div class="col-3 border border-secondary">Truck Number</div> ...

Tips on implementing window scrolling functionality in AngularJS

Despite my extensive search on Stackoverflow for solutions, I have not been able to resolve my issue. I am currently in the process of migrating from jQuery to AngularJS. I need help converting these three lines of code from jQuery to AngularJS: $(windo ...

What is the best way to update a specific value in an object without affecting the rest of

Below is a data object: { name: "Catherine Myer", age: 23, birthday: "august" } If I want to pass this data as a prop to a component, but also change the age to 24, how can I do that? <NextPage data={author.age = 24}/> The ...

Vue - when multiple parents share a common child component

Is there a way in Vue.js for multiple parents to share the same child component? I am looking to have multiple delete buttons trigger a single modal with different content. For example: myfile.html: <table id="app" class="table table-striped table-s ...

Tips for passing navigator reference to React Native's <Drawer/> component?

Utilizing react-native-drawer ( https://github.com/root-two/react-native-drawer ) in my index.ios.js file, I have the following setup where I am attempting to pass the 'navigator' reference into the <DrawerPanel /> of <Drawer /> compo ...