如何从内部有密钥对的对象数组中提取值?

时间:2016-03-05 22:26:20

标签: arrays angularjs object firebase

作为我的应用程序的一部分,我想提取monthlyIncomesavePercent和年份的值,并将其保存为$scope。值,以便在用户登录时将它们设置为默认值!

[
    {
        "$id": "date",
        "$priority": null,
        "$value": 1457178818625
    },
    {
        "$id": "email",
        "$priority": null,
        "$value": "a@b.com"
    },
    {
        "$id": "firstname",
        "$priority": null,
        "$value": "test"
    },
    {
        "$id": "lastname",
        "$priority": null,
        "$value": "Isaacs"
    },
    {
        "$id": "monthly",
        "$priority": null,
        "$value": 328947
    },
    {
        "$id": "monthlyIncome",
        "$priority": null,
        "$value": 123
    },
    {
        "$id": "percent",
        "$priority": null,
        "$value": 10
    },
    {
        "$id": "regUser",
        "$priority": null,
        "$value": "4157e8b1-efa2-4feb-bf75-e907c0e200e0"
    },
    {
        "$id": "savePercent",
        "$priority": null,
        "$value": 10
    },
    {
        "$id": "years",
        "$priority": null,
        "$value": 40
    }
]

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

假设您将数组存储在$scope.data属性中。然后你需要的只是检查这个数组中的每个对象。您可以使用angular.forEach函数循环遍历数组并检查每个对象。

检查此弹药plnkr.co/edit/WeQhCb?p=preview 或以下代码:

//for each object in data array
angular.forEach($scope.data, function(value, key) {
  // check if value id is equals to 'monthlyIncome' OR 'savePercent' OR 'years'
  if (value.$id == 'monthlyIncome' ||
      value.$id == 'savePercent' || 
      value.$id == 'years') {
    // add it's value with the same key (e.g. $scope.monthlyIncome)
    $scope[value.$id] = value.$value;
  }
});

<小时/> 现在它在$scope

monthlyIncome: {{monthlyIncome}}
savePercent: {{savePercent}}
years: {{years}}

<小时/> 或者更优雅的方法来检查属性是在数组中添加必需的道具并检查它是否。完整代码:

(function(angular) {
  'use strict';
  angular.module('app', [])
    .controller('appController', function($scope, $log) {
      $scope.data = [{
        "$id": "date",
        "$priority": null,
        "$value": 1457178818625
      }, {
        "$id": "email",
        "$priority": null,
        "$value": "a@b.com"
      }, {
        "$id": "firstname",
        "$priority": null,
        "$value": "test"
      }, {
        "$id": "lastname",
        "$priority": null,
        "$value": "Isaacs"
      }, {
        "$id": "monthly",
        "$priority": null,
        "$value": 328947
      }, {
        "$id": "monthlyIncome",
        "$priority": null,
        "$value": 123
      }, {
        "$id": "percent",
        "$priority": null,
        "$value": 10
      }, {
        "$id": "regUser",
        "$priority": null,
        "$value": "4157e8b1-efa2-4feb-bf75-e907c0e200e0"
      }, {
        "$id": "savePercent",
        "$priority": null,
        "$value": 10
      }, {
        "$id": "years",
        "$priority": null,
        "$value": 40
      }];

      // required properties for $scope
      var propertieIDs = ['monthlyIncome', 'savePercent', 'years'];

      //for each object in data array
      angular.forEach($scope.data, function(value, key) {
        // check if value id is in 
        if (propertieIDs.indexOf(value.$id) !== -1) {
          // add it's value with the same key (e.g. $scope.monthlyIncome)
          $scope[value.$id] = value.$value;
        }
      });

    });
})(window.angular);