如何比较JavaScript中

时间:2016-07-26 10:54:37

标签: javascript underscore.js

在我的" var"中有一些id? 。该id都存在于静态数据中,该数据同时具有名称的id。我想要做的是我必须找出我的名字的名字并单独打印这个名字。我尝试使用许多Underscore js方法但失败了。有人可以澄清/建议我。



 var CatePeri =[];
 var cate =[];
// var CateName=[];

   $scope.getCategories = function() {
      for (var i = 0 ; i < mainSteps.length; i++) {
          for (var j = 0; j<mainSteps[i].steps.length; j++) {                 
              var CatePeri= mainSteps[i].steps[j].category;
               cate.push(CatePeri);
        // var CateName =_.findWhere(mainSteps[i].category, cate);
        // var CateName =_.where(mainSteps[i].category, cate);
              }
         }
           return cate; // It return Id's Now
                        // But i have to Return the name's of that id's
&#13;
&#13;
&#13;

这里

var CatePeri is having Whole Category id's with Names,
      var cate is having Category id's alone,

我不需要返回id,而是需要返回他们的名字。

2 个答案:

答案 0 :(得分:1)

 var CatePeri =[];
 var cate =[];
// var CateName=[];

   $scope.getCategories = function() {
      for (var i = 0 ; i < mainSteps.length; i++) {
          for (var j = 0; j<mainSteps[i].steps.length; j++) {                 
              var CatePeri= mainSteps[i].steps[j].category;
               cate.push(CatePeri);
        // var CateName =_.findWhere(mainSteps[i].category, cate);
        // var CateName =_.where(mainSteps[i].category, cate);
              }
         }
           return _.pick(_.values(cate), 'name'); // It return Id's Now
                        // But i have to Return the name's of that id's

答案 1 :(得分:0)

我假设你有一个静态JSON,如下所示

var staticJson ={
         id:'5rtf567',
         name:'S'
           }

使用undercore.js _.where选项,例如

   // If you have the multiple object to find out the name put it the below code in `for` loop and try it
   var nameOfId  = _.where(staticJson ,{id:staticJson.id}
   console.log('If you Want Whole Object',angular.toJson(nameOfId));    
   console.log('If you Want Name',angular.toJson(nameOfId.name));