解析$http result以获得AngularJS中的特定项
Parse $http result to get specific items in AngularJS
我从angularJS中的$http GET
得到json样式的结果。它看起来像这样:
{
"meta":{
"limit":1000,
},
"objects":[
{
"custom_pk":"1",
"linked_client":null,
"resource_uri":"/api/v1/card_infos/1"
},
{
"custom_pk":"2",
"linked_client":null
}, ...
我想有一个数组,其中包含所有的custom_pk
值做的事情,如:
$scope.validate_pk = function(val){
if (val in myArray)
// do some stuff
如何创建myArray?
您可以像这样提取对象:
var json = ... the javascript object shown in your question
var custom_pks = [];
for (var i = 0; i < json.objects.length; i++) {
custom_pks.push(json.objects[i].custom_pk);
}
// at this stage the custom_pks array will contain the list of all
// custom_pk properties from the objects property
我更喜欢使用。map()数组函数:
var myArray = json.objects.map(function(item){
return item.custom_pk;
});
Array.map()接受一个函数作为参数。该函数对数组中的每个值执行一次,并将(item, index, list)作为参数传递。map函数的结果是一个包含传递函数结果的新数组。
相关文章:
- 我无法使用angularJs($http)访问服务器
- Typescript angularjs$http获取响应类型(避免使用<any>)
- Angularjs http.get() Cross-Origin 请求被阻止
- AngularJS$http重定向状态代码
- AngularJS$http.post调用返回502(坏网关)错误
- AngularJS$http.post没有'不适用于Chrome,仅适用于IE
- angularJS $http.get communicating with API
- 如果需要登录,Angularjs$http拦截器会暂停异步请求
- AngularJS $http GET 请求到本地 JSON 文件返回语法错误:意外令牌]
- AngularJS$http返回200正常,但失败了
- AngularJS HTTP Get 没有得到响应
- Angularjs$http删除$q promise导致TypeError:对象不是函数
- Angularjs http get from ASP.NET
- AngularJS$http服务中的encodeURIComponent
- 使用AngularJS$http.get在滚动时重复请求JSON数组
- AngularJS http.jsonp请求.then和.success函数不起作用
- 由于浏览历史设置,angularjs http.get调用未在ie10中启动
- 工厂中的AngularJS$http请求
- 无法使用angularjs$http请求将参数发送到服务器操作方法
- 如何通过AngularJS$http从ASP.Net Web API 2获取访问令牌