【问题标题】:How to get a nested object within an array using $meteor.object?如何使用 $meteor.object 在数组中获取嵌套对象?
【发布时间】:2016-02-24 16:23:43
【问题描述】:

数据

countries: [
  {
    id: 'qwe123rty456',
    name: 'Australia',
    regions: [
      {
        id: 'poi098uyt765',
        name: 'Western Australia'
      },
      {
        id: '123poi098qwe',
        name: 'Queensland'
      }
    ]
  }
]

我可以通过以下方式获取特定国家/地区:

$scope.country = $meteor.object(Countries, $stateParams.countryId);

但是我怎样才能得到一个特定的区域呢?如:

{id: '123poi098qwe', name: 'Queensland'}

【问题讨论】:

    标签: angularjs mongodb meteor iron-router


    【解决方案1】:

    似乎$meteor.object accepts only MongoDB objects 作为第一个参数。您可以尝试循环您的结果以找到内部区域:

    $scope.country = $meteor.object(Countries, $stateParams.countryId);
    
    $scope.objectFindByKey(array, key, value) {
        for (var i = 0; i < array.length; i++) {
            if (array[i][key] === value) {
                return array[i];
            }
        }
        return null;
    }
    
    $scope.region = $scope.objectFindByKey($scope.country["regions"], "id", "your_id");
    

    或者尝试使用 Meteor 的 core driver 获取您的 MongoDb 集合

    【讨论】:

    • 不,我收到以下错误:“$meteorObject 的第一个参数必须是函数或具有 findOne 函数属性。”
    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2013-10-11
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多