如何从另一个JSON对象中查找值

时间:2021-10-02 20:10:47

I have set of json objects.

我有一组json对象。

  1. Languages
  2. User Details

User Details have the languages field - which have more than 1 values. Here is my sample json

用户详细信息具有语言字段 - 具有多个值。这是我的样本json

 $scope.languages = [
            {id: 1, text: 'English'},
            {id: 2, text: 'French'},
            {id: 3, text: 'Hindi'},
            {id: 4, text: 'Telugu'}
          ];

    $scope.users = [{name: 'first user', status: 1,language:"1"},
                    {name: 'second user', status: 2,language:"1,2"},
                    {name: 'third user', status: 3,language:"1,3,4"}];

In my view i want to list the user name and languages.

在我看来,我想列出用户名和语言。

<li ng-repeat="user in users">{{user.name}} -  {{testString}}</li>

I know to do for single value. But for multiple values. I have the logic but i don't know how to implement. I am thinking that. First i have to split the user language string and change into array and then find the index of the language id and then return the language text.

我知道要为单一价值做。但对于多个价值观。我有逻辑,但我不知道如何实现。我在想。首先,我必须拆分用户语言字符串并更改为数组,然后找到语言ID的索引,然后返回语言文本。

I have the code to return language name from ID.

我有代码从ID返回语言名称。

var foundItem = $filter('filter')($scope.languages, { id: 3  }, true)[0];
var index = $scope.languages.indexOf(foundItem );
$scope.result = $scope.languages[index].text;

So now the problem is how to print the languages next to the user name?

那么问题是如何打印用户名旁边的语言?

I tried like this

我试过这样的

$scope.testString = function() {
    return "return test string";
}

{{testString}}

But its not working. If this works we can pass the langugae codes as parameter and i can add the search code inside the testString function.

但它不起作用。如果这有效,我们可以将langugae代码作为参数传递,我可以在testString函数中添加搜索代码。

Thanks

2 个解决方案

#1


1  

testString is a function so you cannot use it like {{testString}}, you have to call that function {{testString()}}

testString是一个函数,所以你不能像{{testString}}那样使用它,你必须调用那个函数{{testString()}}

You can simplify your code like this.

您可以像这样简化代码。

    $scope.getLanguages = function (langs) {
      var l = [];
      angular.forEach(langs.split(','), function (lang) {
        l.push(findLanguageTextById(parseInt(lang)));
      })
      return l.join(', ');
    }    
    function findLanguageTextById (langId) {
      for(var i = 0;i<$scope.languages.length;i++) {
        if ($scope.languages[i].id == langId) {
          return $scope.languages[i].text;
        }
      }
    }

I have created a working demo for your problem take a look.

我已经为您的问题创建了一个工作演示。

http://plnkr.co/edit/Cdl8y58IExoVSZV6lp76?p=preview

#2


0  

I think you are not calling the function,

我想你不是在调用这个函数,

$scope.testString = (function() {
    return "return test string";
})();

#1


1  

testString is a function so you cannot use it like {{testString}}, you have to call that function {{testString()}}

testString是一个函数,所以你不能像{{testString}}那样使用它,你必须调用那个函数{{testString()}}

You can simplify your code like this.

您可以像这样简化代码。

    $scope.getLanguages = function (langs) {
      var l = [];
      angular.forEach(langs.split(','), function (lang) {
        l.push(findLanguageTextById(parseInt(lang)));
      })
      return l.join(', ');
    }    
    function findLanguageTextById (langId) {
      for(var i = 0;i<$scope.languages.length;i++) {
        if ($scope.languages[i].id == langId) {
          return $scope.languages[i].text;
        }
      }
    }

I have created a working demo for your problem take a look.

我已经为您的问题创建了一个工作演示。

http://plnkr.co/edit/Cdl8y58IExoVSZV6lp76?p=preview

#2


0  

I think you are not calling the function,

我想你不是在调用这个函数,

$scope.testString = (function() {
    return "return test string";
})();