0

So i have two arrays, One looks a little liek this

$scope.users = {id: 1, email: xxx} {id: 2, email: xxx}....

and i have this one

$scope.booking = {id: 20, regid: 2} ....

So the users is being displayed in a table at the moment.

however if the user has a booking, i want the table row to have a red flag against it (cant share table but its a basic ng-repeat) So if the usersid exists the regid in the booking array, push something to the user

Any help would be wicked

samnymr
  • 91
  • 2
  • 9
  • Please review this article on how to ask a question in StackOverflow: https://stackoverflow.com/help/how-to-ask – Pop-A-Stash Aug 14 '17 at 14:57
  • Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Pop-A-Stash Aug 14 '17 at 15:04

2 Answers2

0

First of All your $scope.users and $scope.booking are objects not arrays...

i make an example how to know if users has book, so heres is the code and the plnkr

the magic is in the foreach and for loops.

HTML

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">

    <table>
      <thead>
        <th>ID</th>
        <th>NAME</th>
        <th>HAS BOOK</th>
      </thead>
      <tbody>
        <tr ng-repeat="user in users">
          <td>{{user.id}}</td>
          <td>{{user.name}}</td>
          <td>{{user.hasBook}}</td>
        </tr>
      </tbody>
    </table>
  </body>

</html>

CONTROLLER

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';


  $scope.users = [
    {id:1, name:'john'},
    {id:2, name:'barney'},
    {id:3, name:'moroni'},
    {id:4, name:'adam'},
    {id:5, name:'sam'}
  ];

  $scope.books = [
    {id:1, regId:1, name:'book1'},
    {id:2, regId:2, name:'book1'},
    {id:5, regId:3, name:'book1'},

  ];

  angular.forEach($scope.users, function(value, index){
    for(var i =0; i< $scope.books.length; i++){
      if(value.id === $scope.books[i].id){
        value.hasBook = true;
      }
    }
  });
  console.log($scope.users);


});
Jesus Carrasco
  • 1,355
  • 1
  • 11
  • 15
-1

You can use filters to find if any user with a conrcrete userid is in booking table. Check this example:

[http://jsbin.com/ewitun/1/edit?html,js,output][1]