[英]JavaScript: How to get specific value from JSON data
I have the following code in which I fetch data from the JSON file , I Store data in $scope.users
variable, But I want to fetch only username value how can I do this? 我有以下代码,其中从JSON文件中获取数据,将数据存储在
$scope.users
变量中,但是我只想获取用户名值,我该怎么做?
LoginCtrl.js LoginCtrl.js
'use strict';
angular.module('User').controller('LoginCtrl', ['$scope','$state', "SettingService","UserService", function($scope,$state, SettingService,UserService) {
$scope.users = [];
UserService.getLoginInfo().then(function(response){
$scope.users = response.data.data["username"];
}, function(error){
})
var vm = this;
vm.doLogin = function(){
var username = document.forms["loginForm"]["email"].value;
var password = document.forms["loginForm"]["password"].value;
if(username == "admin@gmail.com" )
{
if(password == "admin")
{
$state.go('app.home');
}
}
};
}]);
User.json User.json
{
"result": "success",
"data": [
{ "id": 1, "username":"admin@gmail.com", "password":"admin"}
]
}
you can get value for that JSON value as 您可以通过以下方式获取该JSON值的值:
response.data.data[0]["username"]
If you want to get all the usernames from the array, then you can do something like this: 如果要从数组中获取所有用户名,则可以执行以下操作:
var arr = response.data.map(function(a, b){
return a.username;
});
arr
will contain all the username details arr
将包含所有用户名详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.