如何使用 JavaScript/Angular.js 从数组中获取单个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32780645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:44:57  来源:igfitidea点击:

How to get individual value from array using JavaScript/Angular.js

javascriptangularjs

提问by satya

i have a array object which produce below output.

我有一个产生以下输出的数组对象。

console.log('response',response);

response [{"id":"2","name":"subhra","pass":"12345","email":"[email protected]"}]

Here i need all individual value(email,id,pass,name) using JavaScript.Please help me to resolve this issue.

这里我需要email,id,pass,name使用 JavaScript 的所有单个值()。请帮我解决这个问题。

回答by SVK

Updated link

更新链接

var data=[{"id":"2","name":"subhra","pass":"12345","email":"[email protected]"}];
console.log('response',data);

    console.log('response',data[0].id); //2
    console.log('response',data[0].name); //subhra
    console.log('response',data[0].pass); //12345
    console.log('response',data[0].email); //[email protected]

回答by gaurav bhavsar

You can use angular.forEach

您可以使用 angular.forEach

Working Plunker

工作Plunker

Controller

控制器

$scope.response = [{"id":"2","name":"subhra","pass":"12345","email":"[email protected]"}]

angular.forEach($scope.response, function(item){
    $scope.Id = item.id; // id is in $scope.Id
    $scope.Name = item.name; // name is in $scope.Name
    $scope.Email = item.email; // email is in $scope.Email
    scope.Pass = item.pass;   // pass is in $scope.Pass
});

HTML

HTML

<body ng-controller="MainCtrl">
    <p> ID : {{Id}}</p>
    <p> Name : {{Name}}</p>
    <p> Pass : {{Pass}}</p>
    <p> email : {{Email}}</p>
</body>