【问题标题】:Angularjs:Getting rows from array that matches some conditionAngularjs:从匹配某些条件的数组中获取行
【发布时间】:2015-11-12 14:48:31
【问题描述】:

我有一个像这样的数组

var hospitals=[{"distric_id":"1","type":"c","details":[{"name":"hello","address":"hello","number":"686678"}]},
   {"distric_id":"1","type":"g","details":[{"name":"hello","address":"hello","number":"686678"}}]}
   ]

现在我需要从满足某些条件的数组中获取行。 这是代码sn-p

 //loop through the array and fecth some rows based on some condition
function getHospitals(id,type){
  var hospitals=[{"district_id":"1","type":"c","details":[{"name":"hello","address":"hello","number":"686678"}]},
       {"district_id":"1","type":"g","details":[{"name":"hello","address":"hello","number":"686678"}}]}
       ]
  //return array of results
  return result;
}

result 数组包含来自hospitals 数组的符合条件的行 像 select * from where district_id=id and type=type 那么如何从上面的数组中选出符合上述条件的一行呢?

【问题讨论】:

    标签: javascript html arrays angularjs


    【解决方案1】:

    您可以使用 Lodash 来执行此操作。 https://lodash.com/docs#findWhere

    来自 lodash:

    var users = [
      { 'user': 'barney', 'age': 36, 'active': true },
      { 'user': 'fred',   'age': 40, 'active': false }
    ];
    
    _.result(_.findWhere(users, { 'age': 36, 'active': true }), 'user');
    // → 'barney'
    
    _.result(_.findWhere(users, { 'age': 40, 'active': false }), 'user');
    // → 'fred'
    

    lodash中有很多搜索函数:find() findIndex(), ...

    【讨论】:

      【解决方案2】:

      目前还不存在基于 JavaScript 数组的成熟 SQL 解析器和存储系统。有一些已弃用和实验性的东西像this sql parser 这样只支持 SELECT 语句,但我强烈建议不要这样做。

      相反,您可能想看看lodash(或underscore)。它们都有很多高级实用功能,可以让处理数组和其他类型的集合变得更加容易。

      例如

      select * from where district_id=id and type=type

      可以这样实现:

      var matchingHospitals = _.where(hospitals, {district_id: id, type: type});

      注意:_.where 返回一个数组。

      另外,如果_.where不剪,还有_.filter_.find等等……

      【讨论】:

      • 有 angularjs api 吗?
      • @shammon 完全独立于 Angular。您不需要任何特定于 Angular 的东西,因为您的数据存储在纯 JavaScript 对象中。有许多库可以更轻松地处理 JavaScript 对象和数组。 Lodash 和 Underscore 是最受欢迎的。
      【解决方案3】:

      你可以使用 jQuery 的 'grep' 函数来完成:

      function getHospitals(id, type){
          var hospitals=[
              {"district_id":"1","type":"c","details":[{"name":"hello","address":"hello","number":"686678"}]},
              {"district_id":"1","type":"g","details":[{"name":"hello","address":"hello","number":"686678"}}]}
          ];
      
          //return array of results
          return $.grep(hospitals, function(e){
              return (e.district_id == id && e.type == type);
          });
      }
      

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:
            //loop through the array and return all rows satisfying the conditions
            function getHospitals(id,type){
          var hospitals=[{
            "district_id":"1","type":"c","details":[{"name":"hello","address":"hello","number":"686678"}]
          },{"district_id":"1","type":"g","details":{"name":"hello","address":"hello","number":"686678"}}];
          var matchingHospitals = []; //an array to save all the matching hospitals that satisfy the condition
          for(var i in hospitals){  //loop through the data
            var hospital = hospitals[i];
            if(hospital.district_id==id && hospital.type==type){ //check condition
              matchingHospitals.push(hospital);    //push into matching array
            }
          }
        
          //return array of results
          return matchingHospitals;   //return matching hospitals array
        }
        

        测试查询:

        getHospitals("1","c")
        

        【讨论】:

        • 已更新,包含测试代码。该代码使用纯 JavaScript,不需要额外的库或插件。代码可以缩短,但我喜欢简单和更好的理解能力:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-12
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多