简体   繁体   English

从JSON获取基于另一个字段的值而无需迭代-angular js

[英]getting values based on another field from json without iteration - angular js

How to get "value" from "name" from a json without iteration in Angular JS . 如何从json中的“名称”中获取“值”而不在Angular JS进行迭代。 For example I 've the following array with name and value field, how can I retrieve say "ABC" value based on name "a" without iteration in script?. 例如,我具有名称和值字段的以下数组,如何在脚本中不进行迭代的情况下基于名称“ a”检索“ ABC”值?

$scope.array = [{"name":"a","value":"ABC"},{"name":"b","value":"GHI"},{"name":"c","value":"EFG"}];

Can we do anything like this? 我们可以做这样的事情吗?

$scope.value = $scope.array.name["a"].value;

You can use filter instead of iterating: 您可以使用filter而不是进行迭代:

$scope.array.filter(function(obj){ return (obj.name=="a"); })[0].value;//ABC

Although it also iterates over the array. 尽管它也会遍历数组。

You can also use Underscore.js with these kinds of case, it has a find function like this: 您还可以在以下情况下使用Underscore.js ,它具有如下查找功能:

_.find($scope.array, function(item) {
    return item.name == "a";
});

But the point is, all these features, use kind of iteration to give you what you want. 但关键是,所有这些功能都使用某种迭代来提供您想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM