简体   繁体   中英

Angular.js / Javascript: checking if an object is found in any of the elements of an array

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?

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); 

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));

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';
});

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/

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);

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;
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM