2016-03-05 35 views
-3

作爲我的應用程序的一部分,我想提取的值爲monthlyIncomesavePercent和年,並將它們保存爲$scope。值,以便在用戶登錄時將它們設置爲默認值!如何從內部具有密鑰對的對象數組中拉取值?

[ 
    { 
     "$id": "date", 
     "$priority": null, 
     "$value": 1457178818625 
    }, 
    { 
     "$id": "email", 
     "$priority": null, 
     "$value": "[email protected]" 
    }, 
    { 
     "$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 
    } 
] 

任何幫助將不勝感激。

+0

這不是可執行代碼。 – wvdz

+0

我用完「旗幟」,請重新考慮您的問題。很難理解和理解你想要做什麼。 –

+0

問題太模糊了。如果數據已經被傳遞到範圍,那麼顯示的數據在哪裏使用?爲什麼首先這不是一個對象? – charlietfl

回答

1

可以說你在$scope.data屬性中存儲你的數組。然後你需要檢查這個數組中的每個對象。您可以使用angular.forEach函數來遍歷數組並檢查每個對象。

入住這plunker 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": "[email protected]" 
     }, { 
     "$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); 
+1

非常感謝!這正是我之後的:) – worc

相關問題