Extract information from a key-value pair system and then contrast it with the values obtained from looping through a collection in AngularJS

Two fundamental inquiries:

1) Within my controller, I have defined a list with key-value pairs and I am trying to access the event name based on the eventID in order to log it in the console.

$scope.events = [
  {eventID:3200, eventName:"Event One"},
  {eventID:3201, eventName:"Event Two"}
];

I attempted the following code:

 if($scope.events.eventID == 3200) {
    console.log("Ok");
  }

However, it did not work as expected.

2) In my view, there is a table displaying data retrieved from a web service. My goal is to display the description in the table based on the eventID from the $scope.events list. There is a field called event within my ng-repeat that can be used for comparison, but I am unsure how to achieve this.

<tr ng repeat d in data>
  <td>d.Description</td>
  <td>d.anyValue</td>
</tr>

Any assistance would be greatly appreciated. I am new to Angular and in need of guidance :)

Answer №1

Implement controller method

$scope.filterEvents = function(obj){
  return obj.eventID  === 3200;
}

Integrate it into the view

<tr ng-repeat="event in events | filter:filterEvents">
  <td>{{ event.title }}</td>
  <td>{{ event.date }}</td>
</tr>

Answer №2

To tackle question number 1, give this a try:

if($scope.events[0].eventID  == 3200) {
  console.log("Ok");
}

If you want to loop through using this method:

angular.forEach($scope.events, function (value) {
  if (value.eventId === 3200) {
  {
    console.log('OK');
  }
});

For the second question, create a helper function that retrieves the eventName based on the ID.

$scope.getEventName = function (eventId) {
  var result = '';
  angular.forEach($scope.events, function (value) {
    if (value.eventID === eventId) {
      result = value.eventName;
      return false;
    }
  });
  return result;
}

<tr ng-repeat="d in (exportDataEventDesc.result.slice(5, exportDataEventDesc.result.length-2))">
  <td>{{d.Description}}</td>
  <td>{{d.anyValue}}</td>
  <td ng-bind="getEventName(d.merchMetrics.EVENT_ID)" style="text-align: left"></td>
</tr>

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

Incorporating a function from a separate .js file into an index.ejs view using app.js

graphs.js: contains a function that initiates an API call and retrieves an object containing an HTML link for embedding a graph. app.js: includes the following (graphs.js has been imported): var express = require("express"); var app = express(); var grap ...

Ways to modify the color of cells in a table generated from JSON

In developing an HTML table based on JSON data, I have created a university semester map that displays student information including their ID, year, term, and required courses for graduation. While the table is successfully created, I aim to customize the ...

Vue.js - the value of this is not defined

<div class="content-chart"> <chart :type="'line'" :data="lineData" :options="options"></chart> </div> In the template section above, a component is defined to render a chart using Chart.js. The script below handles the l ...

generate a customized synopsis for users without storing any data in the database

In order to provide a summary of the user's choices without saving them to the database, I want to display it in a modal that has already been created. Despite finding some sources online, none of them have worked for me so far. Below is my HTML: &l ...

Why isn't my AJAX POST request to PHP functioning correctly?

It was all working perfectly fine earlier, but now something seems off and I must have made a mistake somewhere. I'm in the process of setting up a form where the information entered is sent via AJAX to my PHP file, which then outputs the username an ...

Find and Scroll Dropdown Menu with Bootstrap Framework

Access the jsfiddle for more information. Have a look at these questions: In my dropdown menu, I have included a search box and multiple sub-menus. However, the search box only filters the first dropdown menu and does not work with the sub-menus. How ca ...

I utilized the `<script src="sample.pdf"></script>` tag in my HTML code and surprisingly, the JavaScript within the PDF document was still able to execute

Recently, I encountered a situation where I included a PDF file with JavaScript code in the src attribute of a script tag in my code. Surprisingly, the JavaScript code executed without any issues. This made me wonder if I can use any type of file extension ...

How to align an image in the center of a circular flex container

I'm facing an issue in my Angular project where I have the following code snippet: onChange(event: any) { var reader = new FileReader(); reader.onload = (event: any) => { this.url = event.target.result; }; reader.readAsData ...

Ensuring the dropdown menu's functionality in AngularJS

I'm currently working on a code snippet that validates when the user moves away from a specific select element. The goal is to change the border color of the select element to either red or green only when the user tabs away from it or moves the mouse ...

Shuffle Image and Color Combinations on Website

I want to create a feature on my website where an image and color pair are randomized every time the page is refreshed. For example, upon refreshing, it may display "img A" with color "#999;", and on the next refresh, it might show "img B" with color "#fff ...

Failed Axios POST request on iOS devices

Attempting a straightforward ajax POST from domain1 to domain2 using Axios. This involves a cross-domain simple POST without requiring a PREFLIGHT (OPTIONS) call. The response returned by the application is a basic JSON string. This process functions smoo ...

What is the correct way to utilize the BigInt right shift operator and XOR in JavaScript?

After spending the entire day trying to figure out how to shift bits to the right in JavaScript, I realized that my test program written in C language was not giving the correct results for comparison. If anyone can guide me in the right direction, please ...

Executing selenium tests on Internet Explorer 11 on a Windows 10 1809 machine without encountering any new pop-up windows

While testing on my computer, I encountered an issue where my test would start successfully, but after opening and closing several Internet Explorer windows during the test, no new windows would open. There were no error messages displayed, and the test se ...

Issue with button style in dropdown menu in Twitter Bootstrap

<nav class="nav-collapse user"> <div class="user-info pull-right"> <img src="http://placekitten.com/35/35" alt="User avatar"> <div class="btn-group"> ...

Modifying state within useEffect while also including the redux value as a dependency array

I am facing an issue with my Redux array and the dependency array in my useEffect. The problem arises when I store the value of the Redux array in a variable using useSelector, which is then used as a dependency in my useEffect. The logic inside the useE ...

rearranging items from one list to another list

I've encountered an issue in my HTML code that I need help with. I have included some classes for clarity, but their specific names are not essential. My goal is to make the "sub-nav" element a child of the "mobile parent" list item on mobile devices. ...

Exploring the power of Ajax within the MVC framework

I'm attempting to incorporate a view through Ajax, but I'm encountering some issues. My knowledge of Ajax is limited, but I am eager to learn. What could be the missing piece here, or am I completely off track with my approach? foreach(ect.. ...

AngularJS UI-calendar fails to display updated events on Calendar

I am currently using Angular UI-Calendar to display events on the Calendar. The events are appearing correctly on the Calendar, but when I update the details of an event, the changes are not reflected in the Calendar display (for example, the start date). ...

jQuery plugin modifies keypress events

I have integrated the Jquery ImgAreaSelector plugin into my website. Within my site, I have implemented several keypress triggers using jquery. For example: $(document).bind('keypress', 'S', function(){ alert("You have pressed S key ...

Tips on expanding the row number column width in jqGrid

Within my JQuery Grid, I am utilizing a parameter called rownumbers. jQuery("#dateInfo").jqGrid({ url : theURL, datatype : "json", sortable: false, colNames:['Date & Time'], colModel:[{name:'arrivalTime',index:'arrivalTime& ...