0

I have an array that is structured like this:

$scope.roomlist = [
    {"roomid":"1", "tablecount":"10", "chaircount":"20", "whiteboards":"2"}, 
    {"roomid":"2", "tablecount":"15", "chaircount":"30", "whiteboards":"2"}, 
    {"roomid":"3", "tablecount":"10", "chaircount":"20"}];

The array values are dynamic and the array is generated by joining some tables. Whiteboards is one of the elements that may exist in all the rooms returned in the roomlist, may exist in some of the rooms as shown above or none of the rooms at all.

All I need to do is test to see if there is at least one room in the list which has the whiteboard object.

I have tried the indexOf test but it returns -1.

The code I executed, was

var myrooms = $scope.roomlist; 
var results = myrooms.indexOf("whiteboards");

And then

console.log(results);

Any suggestions?

1
  • 2
    Your roomlist is an array of dictionaries, but you're indexing a string, the result is always going to be -1, meaning not found. Commented Aug 17, 2016 at 4:42

6 Answers 6

1

You need to iterated room by room, and then check for existence.

function searchRoomsFor(objectName, roomList) {
  var found = false;
  for (var roomIndex in roomList) {
    var room = roomList[roomIndex];
    if (objectName in room) {
      found = true;
    }
  }
  return found;
}

// Check for an object
console.log(searchRoomsFor('whiteboards', $scope.roomlist));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I replaced false and true with 0 and 1 to put conditional logic in my presentation layer.
1

If you just want to check there is a room with at least one whitboard, then you can simply use Array.prototype.some:

var roomlist = [
    {"roomid":"1", "tablecount":"10", "chaircount":"20", "whiteboards":"2"}, 
    {"roomid":"2", "tablecount":"15", "chaircount":"30", "whiteboards":"2"}, 
    {"roomid":"3", "tablecount":"10", "chaircount":"20"}
];

var anyRoomWithWhiteBoard = roomlist.some(room => room.whiteboards && parseInt(room.whiteboards) > 0);

console.log(anyRoomWithWhiteBoard);

Comments

1

you can use lodash library to find out if the whilteboard exist or not

$scope.roomlist = [{
    "roomid": "1",
    "tablecount": "10",
    "chaircount": "20",
    "whiteboards": "2"
   }, {
   "roomid": "2",
   "tablecount": "15",
   "chaircount": "30",
   "whiteboards": "2"
   }, {
   "roomid": "3",
   "tablecount": "10",
   "chaircount": "20"
}];

var myrooms = $scope.roomlist;
w = _.find(myrooms, function(r) {
    return r.whiteboards
});
if (w) {
    alert("there is a whiteboard");
} else {
    alert("there is no white board")
}

https://jsfiddle.net/phpforyou/jv5hzsvh/

1 Comment

this works too. But i just decided to go with the loop.
1

If you're trying to find the first index of a key in an array of objects you need to perform a loop.

var index = -1;
for (var i = 0; i < $scope.roomlist.length; i++) {
    // check if the whiteboards property is defined
    if (typeof roomlist[i].whiteboards !== 'undefined') {
        var index = i;
        break; // we've found one, stop looping
    }
}
console.log(index);

There is new, and poorly support, array.findIndex() method which can simplify the previous loop to the following. It calls a function for each item and returns the index of the first item where the function returns true.

var indexOfWhiteboardRoom = $scope.roomlist.findIndex(function(room, index, arr) {
    return typeof room.whiteboard !== 'undefined';
});    

If you want to find the first room with a whiteboard you can use with array.find(). Which calls a function for each item in the array and returns the first item where the function returns true.

var whiteboardRoom = $scope.roomlist.find(function(room, index, arr) {
    return typeof room.whiteboard !== 'undefined';
});

If you're trying to find all the objects that have a whiteboard property defined, you can use array.filter(). Which calls a function for each item in the array and returns only the items where the result of the function is true.

var whiteboardRooms = $scope.roomlist.filter(function(room, index, arr) {
    return typeof room.whiteboard !== 'undefined';
});

If you just want to know if a whiteboard exists.

var whiteboardExists = $scope.roomlist.some(function(room, index, arr) {
    return typeof room.whiteboard !== 'undefined';
});

2 Comments

Array.prototype.some checks if whiteboards exist. so maybe better to change the statement about it (If you want to find the first room with a whiteboard...).
I already did. I was thinking of array.find() when I wrote it.
0

You Can Try This Similar As Your Question

var arr = [1, 2, 3];
var check = [3, 4];

var found = false;
for (var i = 0; i < check.length; i++) {
if (arr.indexOf(check[i]) > -1) {
    found = true;
    break;
}
}
console.log(found);

Comments

0

You wont able to get indexOf('whiteboards') in $scope.roomlist because whiteboards is not direct element of the array roomlist. So you have to iterate it. And using AngularJS angular.forEach() is the best way to do.

var hasWhiteboards;
angular.forEach($scope.roomlist, function (list) {
  if (list.whiteboards) {
    hasWhiteboards = true;
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.