Exploring Angular's JavaScript Find Function

Here is a snippet of the code I've been working on:

   var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

myApp.controller('MyCtrl', ['$scope', MyCtrl]);

function MyCtrl($scope) {
    $scope.name = 'Superhero';

    $scope.names = [
        {
            "name": "AAAAAA",
            "down": "False"
        },
        {
            "name": "BBBBBB",
            "down": "45%"
        },
        {
            "name": "CCCCC",
            "down": "12%"
        }
        ];

    $scope.datas = [
        {
            "data": "AAAAAA/45%"
        }
        ];

    $scope.getTheRightData = data => $scope.datas.map(d=>d.data.split('/')[0]).find(d=>d===data);
}

Some HTML

<div ng-controller="MyCtrl">
  <table>
        <tbody>
          <tr ng-repeat="name in names">
            <td>{{name.name}}</td>
            <td>{{name.down}}</td>
            <td ng-bind="getTheRightData(name.name)"></td>
          </tr>
         </tbody>
  </table>

In my current setup, an element from $scope.datas should match with an element from $scope.names, but it only displays when name.name matches. I am trying to implement a scenario where not only name.name should match, but also name.down. This involves using something like this:

<td ng-bind="getTheRightData(name.name,name.down)"></td>
, and modifying the controller function accordingly

$scope.getTheRightData = data => $scope.datas.map(d=>d.data.split('/')[0][1]).find(d=>d===data);
}

However, this approach is not functioning as expected. I would appreciate any suggestions or insights on how to resolve this issue. Thank you!

Answer №1

I have provided a sample answer below.

var myApp = angular.module('myApp', []);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

myApp.controller('MyCtrl', ['$scope', MyCtrl]);

function MyCtrl($scope) {
  $scope.name = 'Superhero';

  $scope.names = [{
    "name": "AAAAAA",
    "down": "False"
  }, {
    "name": "BBBBBB",
    "down": "45%"
  }, {
    "name": "CCCCC",
    "down": "12%"
  }, {
    "name": "AAAAAA",
    "down": "45%"
  }];

  $scope.datas = [{
    "data": "AAAAAA/45%"
  }, {
    "data": "CCCCC/12%"
  }];

  $scope.getTheRightData = data => $scope.datas.map(param => {
    return {
      name: param.data.split('/')[0],
      down: param.data.split('/')[1]
    }
  }).find(param => param.name == data.name && param.down == data.down);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="myApp">

<head>
  <title></title>
  <meta charset="utf-8" />
</head>

<body>
  <div ng-controller="MyCtrl">
    <table>
      <tbody>
        <tr ng-repeat="name in names">
          <td>{{name.name}}</td>
          <td>{{name.down}}</td>
          <td>{{getTheRightData(name)}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

</html>

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 there a way for me to determine the dimensions of the webcam display?

How do I determine the width and height of the camera in order to utilize it on a canvas while preserving proportions? I am attempting to ascertain the dimensions of the camera so that I can use them on a canvas. On this canvas, I plan to display live vid ...

Is it possible to dynamically load a controller in Angular.js using require.js or any alternative method?

Is it possible to dynamically load a controller in Angular.js using require.js or any other method? I would greatly appreciate your assistance with this. ...

Troubleshooting issue with error handling in graphql mutation hook with react and apollo is not resolving

It seems like I might have overlooked a configuration in my code, but I can't seem to pinpoint where I went wrong. In our application, we have a basic login form. If the correct username and password are entered, everything works smoothly. However, ...

Executing a function from another reducer using React and redux

My application consists of two main components: the Market and the Status. The Status component manages the user's money, while the Market component contains buttons for purchasing items. My goal is to decrement the user's money when a button in ...

Ways to Determine the Height and Width of an Image Once it has been Adjusted for a View

Is there a way to retrieve the height and width of an image after it has been resized for a view? I have images that may vary in original dimensions, but users can resize them as needed. For example, this code from the console gives the client height: do ...

VueJS1 flexible $parent.$parent method duration

Trying to utilize a parent function across multiple layers of nested child components. {{ $parent.$parent.testFunction('foo', 'bar') }} This approach currently functions, however, each time I navigate through levels in the hierarchy, ...

Guide on executing a service call using an Angular-UI modal object

I have recently decided to switch to an Angular-UI modal and I'm facing some confusion about how to execute a $http get call and retrieve the results. Previously, I was using a different Angular modal with the existing code. While I understand how it ...

What is the most effective method for starting an application from a web browser, regardless of platform or browser used?

Forgive me if this question has been asked before in a similar way. I have multiple test applications that operate on different platforms such as Windows 95, Windows XP, SUSE, RedHat, and other variations of UNIX. Currently, the process involves a native a ...

Error encountered in Angular JS: $parse:syntax Syntax Error detected

var app=angular.module('myapp',[]) app.controller('myctrl',Myfunction); function Myfunction($scope,$compile){ var self=this; $scope.text=s4(); $scope.adding=function(){ var divElement = angular.element($("#exampleId")); ...

What issues are present in the Ajax script and the PHP radio input?

I'm having trouble extracting the value of a radio input in this code so I can update the database: <script type="text/javascript> function getVote() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlh ...

retrieval: unspecified information obtained through body parsing (Node.js Express)

Having just started working with React.js and Node.js, I encountered a simple issue that I can't seem to solve. I am using the lightweight fetch API to post data and trying to receive that data using body-parser, but it always returns undefined. impo ...

How about generating a promise that serves no real purpose and simply resolves?

Managing promises in Node.js can be a bit tricky, especially when dealing with different scenarios. One common use case is catching the last promise result and formatting it accordingly. req.resolve = (promise) => { return promise.then(() => { ...

Triggering onClick without interfering with its populated variable

I'd like to add the following code snippet to my document: $('#myDiv).append("<div id='myDiv2' onclick="+extElementConfig.onClickDo+">Do</div>"); The code above uses an object with properties to populate the onClick attrib ...

Despite awaiting them, promises are not resolving synchronously

I have a function that retrieves location information and returns a promise. I use mobx to manage the store, updating the this.locationStoreProp and this.hotel.subtext properties. public fetchPropertyLocation(some_input_params): Promise<any> { ...

The functionality of Select2 is experiencing issues within the popup of a chrome extension

Encountering an issue with Select2 behavior while using a chrome extension with AngularJS and Angular-UI. The situation Select2's content is being loaded asynchronously through the AngularJs $resouces module. Expected outcome The content should be ...

The module located at "c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx" does not have a default export available

I am currently delving into the realm of RxJs. Even after installing rxjs in package.json, why am I still encountering an error that says [ts] Module '"c:/Users//Desktop/iooioi/src/main/webapp/node_modules/rxjs/Rx"' has no default export ...

Reacts Router Link does not refresh page content

My React and Redux application features a Movie component that displays movie data fetched from an API. To enhance user experience, I decided to create a Similar Movies section at the bottom of the page. This section contains components that allow users t ...

Using JavaScript to Render an Array in HTML through Looping (with JSON)

I am facing an issue where only the first value of my associative array is being returned when I loop through it in my HTML file. However, when I check it using console.log, all data is displayed correctly. There is no error displayed in the HTML, but not ...

What is the process for creating a wait condition specifically for browser.title()?

Can someone assist me with finding a way to wait for the browser title? I'm currently using browser.getTitle() but my script is timing out. Unfortunately, I cannot use browser.sleep in this situation. Is there a way to achieve this using browser.wait( ...

Can you tell me the specific parameter used in the beforeSend function in jQuery?

I have been examining some instances of the beforeSend callback function. Sometimes, these examples include an input parameter: beforeSend:function(req) or beforeSend:function(xhr). I assume that this parameter represents the XMLHTTPRequest of the jquery ...