search

Home  >  Q&A  >  body text

angular.js - filter numbers, whether it can be filtered accurately

datas structure is similar to

[{"id":1,"name":"test"},{"id":13,"name":"test2"}]

Accurate filtering by id,
The following writing method will filter out all the data containing 1 in the id (for example, the above two will be filtered out), can precise filtering be done?

datas|filter:{id : 1}
某草草某草草2779 days ago583

reply all(4)I'll reply

  • ringa_lee

    ringa_lee2017-05-15 17:08:08

    var target = 1;
    var fitFun = function(data,target){
      var result = []; 
    angular.forEach(data,function(value,key){
      if(value.id==target){result.push(data[key])};
    })
    return result; 
    };  
    var t = fitFun(data,target);
    console.log(t); 

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 17:08:08

    .filter('idon1',function(){
            return function(arr){
                var returnArr = [];
                var index = '1';
                angular.forEach(arr,function(data,i){
                    if(index.indexOf(data.id) != -1){
                        returnArr.push(data);
                    }
                })
                return returnArr;
            }
        })

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-15 17:08:08

    The official has its own filtering specified attributes, which are included in the official examples
    https://docs.angularjs.org/ap...

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-15 17:08:08

    I agree with the above, just use the official ones. For example, the official filters cannot meet the requirements, such as | date: There is no filtering of dates into Chinese, so just use JS to write a .filter filter yourself

    reply
    0
  • Cancelreply