Looping through an array using NgFor within an object

I've been working on pulling data from an API and displaying it on a webpage.

Here is the JSON structure:

{page: 1, results: Array(20), total_pages: 832, total_results: 16629}
page: 1 
results: Array(20) 
0:
adult: false
backdrop_path: "/xDMIl84Qo5Tsu62c9DGWhmPI67A.jpg"
genre_ids: (3) [28, 12, 878]
id: 505642
original_language: "en"
original_title: "Black Panther: Wakanda Forever"
overview: "Queen Ramonda, Shuri, M’Baku, Okoye and the Dora Milaje fight to...
popularity: 7141.639
poster_path: "/sv1xJUazXeYqALzczSZ3O6nkH75.jpg"
release_date: "2022-11-09"
title: "Black Panther: Wakanda Forever"
video: false
vote_average: 7.5
vote_count: 2879
1
2
3
4
5
6

Component Setup

import { Component } from '@angular/core';
import { tap } from 'rxjs';
import { MovieDataService } from '../services/movie-data.service';

@Component({
  selector: 'home-card',
  templateUrl: './home-card.component.html',
  styleUrls: ['./home-card.component.css']
})
export class HomeCardComponent {

  movieData: any = {};
  constructor(private movieDataService: MovieDataService) {}

  ngOnInit(): void {
    this.movieDataService.getData().subscribe((data) => {
      this.movieData = data;
      // Output JSON data to console
      console.warn(data);
    })
  }
}

Template Markup

<ul>
    <li *ngFor="let item of movieData.results | keyvalue">
        Key: <b>{{item}}</b>
    </li>
</ul>

The data renders successfully on the page as shown in the https://i.sstatic.net/H9Pb9.png.

However, when I attempt to access the title field of an object, I encounter the error: Property title does not exist on type KeyValue<unknown, unknown>.

This is how I'm trying to access the title field:

<ul>
    <li *ngFor="let item of movieData.results | keyvalue">
        Key: <b>{{item.title}}</b>
    </li>
</ul>

I believe I may be overlooking something obvious, but after extensive research, I haven't been able to resolve it yet.

Answer №1

To utilize the keyvalue pipe, it is essential to remember that it only works for objects. To implement this, adjust your code as follows:

<ul>
    <li *ngFor="let item of movieData.results">
        Key: <b>{{item.title}}</b>
    </li>
</ul>

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

Where is the best location to store types/interfaces so that they can be accessed globally throughout the codebase?

I often find myself wondering about the best place to store types and interfaces related to a specific class in TypeScript. There are numerous of them used throughout the code base, and I would rather not constantly import them but have them available gl ...

Issue: A React component went into suspension during the rendering process, however, no alternative UI was designated

I'm currently experimenting with the code found at https://github.com/adarshpastakia/ant-extensions/tree/master/modules/searchbar Although I followed the tutorial instructions, I encountered an error. Could it be that the library is malfunctioning? I ...

Troubleshooting connection issues with a Chat app using Socket.io and Express: How to fix ERR

Currently, I'm immersed in a tutorial that delves into creating a rock-paper-scissors game with an integrated chat feature using socket.io and express. My focus is solely on crafting the chat component. However, a frustrating error keeps cropping up w ...

The data insertion query in MYSQL seems to be malfunctioning

I am facing an issue with inserting data from a form into a database table named "sumation". Despite using PhpStorm IDE, I am getting errors indicating that no data sources are configured to run the SQL and the SQL dialect is not set up. Can anyone help ...

Verify whether an object possesses all the attributes of a class in TypeScript

Within my typescript code, I have a class called abc: export class ABC{ public a : any; public b : any; public c? : any; public d? : any; } In one of my functions, I receive an input which is represented as data:any. My goal is to verify i ...

How to integrate a jQuery plug-in into your Meteor 1.7.0.3 project

Recently, I delved into using Meteor for the first time, and it has been about a week since I started exploring its functionalities. However, I encountered an issue with integrating jQuery plugins that were installed via npm. After running: meteor npm i ...

Creating a Distinct Interior Array Separate from the Exterior

I'm currently working on a project that involves creating a 2D array. I want the interior elements of this array to be black while the exterior elements should be white. However, my 2D array doesn't seem to be forming correctly - it looks more li ...

What is the best way to conceal a div when there are no visible children using AngularJS?

I need help with hiding a parent div only if all the child items are invisible. I have successfully hidden the parent when there are no children, but I am struggling to figure out how to hide it based on visibility. <div ng-repeat="group in section.gro ...

Angular 4 seems to be experiencing some issues with the EventEmiitter functionality

Hey everyone, I'm new to working with Angular 4 and I've been trying to implement the event emitter concept without success. Here's the code I have in my demo: app.component.ts import { Component } from '@angular/core'; @Compon ...

I'm curious, are there any html rendering engines that can display text-based content using curl-php?

When utilizing PHP cURL to interact with webpages, I often find myself needing to use regular expressions if the page contains AJAX and JavaScript elements. Does anyone have any recommendations for rendering HTML pages and extracting the text-based render ...

Utilizing the Sheet Elite API - Step-by-Step Guide for Sending Data to a Designated Sheet Through a POST Request

Recently, I've been working on a project that involves using the Sheet Best API to submit data directly to Google Sheets. However, I'm running into an issue where the data is only being sent to the first sheet out of three. Despite having all the ...

Securely store files by encrypting them with Node.js before saving to the disk

At the moment, I am making use of the multer library to store files on the File system. This particular application is built using Node and Express. I currently have a process in place where I save the file on the server first and then encrypt it. After e ...

Having trouble retrieving the date value from a datepicker in Angular ngForm

I am struggling to fetch a value from the mat-datepicker in my Angular project. Whenever I try, it returns as undefined. I have implemented ngForm and ngModel, but I seem to have made a mistake somewhere. <form #f="ngForm" (ngSubmit)="registerClient(f. ...

Access a portion of the redux state during server requests

I am facing a scenario where I need to make a server call using the most recent redux state. My initial thought was to pass a copy of the state through the method flow and then invoke the action creator with that state. However, there is a chance that the ...

Setting up Bootstrap 5 with the latest version of Angular, which is Angular

I attempted to install Bootstrap in Angular 13 using the following link: However, after updating to bootstrap v5, the styles are not displaying correctly. Interestingly, it works fine with bootstrap 4.6.1. I followed all the steps in the provided link. I ...

Encountering a Javascript Error when trying to begin

As a beginner in Javascript, I am venturing into designing a simple website that incorporates Javascript. Currently, my focus is on building a calculator. However, upon loading my website, nothing seems to initiate and there are no error messages displayed ...

Integrating Dynamics CRM with an External Source to Trigger Workflows

Scenario: Imagine a scenario where you want to trigger an existing workflow or a custom action from a webpage located outside the CRM Dynamics environment, such as MS CRM 2011-2013-2015-2016 and 365. Potential Solution: One possible solution could be to ...

When a specific function is called, the DayPickerRangeController in the airbnb/react-dates library will update the

Is it possible to dynamically set the visible month in DayPickerRangeController after the component has been rendered? I have a 'handleMonthChange' function that I want to use to change the visible month, but setting 'initialVisible' mo ...

How to use the Angular 7 CLI in a programmatic way

My goal is to set up a server that can launch an Angular 7 app. Another scenario where this capability would be useful is for more advanced generation tasks, such as adding multiple files directly after generating an Angular project. const angularClient = ...

Ways to populate missing cells with a default hyphen symbol

Looking for a way to default empty cells in my primeng datatable to '-'. Consider the following data: [ { 'column': null }, { 'column': { 'name': 'A' } }, { 'column': { 'name': ...