gpt4 book ai didi

angularjs - Angularjs Controller 中的过滤器数组

转载 作者:行者123 更新时间:2023-12-02 04:11:33 25 4
gpt4 key购买 nike

我有示例 Json。根据关键值,我更适合。

拟合器没有对精确值进行排序

示例:

angular.module('myApp').controller('testController', ['$filter', '$scope',
function($filter, $scope) {

var obj = '[{"id":"1","m_id":1,"value":"Male"},
{"id":"2","m_id":1,"value":"Female"},
{"id":"3","m_id":1,"value":"Other"},
{"id":"45","m_id":9,"value":"Single"},
{"id":"46","m_id":9,"value":"Married"},
{"id":"47","m_id":10,"value":"Father"},
{"id":"48","m_id":10,"value":"Mother"},
{"id":"61","m_id":10,"value":"Cousin"},
{"id":"62","m_id":10,"value":"Other"}]';

var obj1 = JSON.parse(obj);
var result = $filter('filter')(obj1, {
m_id : "1"
});
}]);

输出:

[{"id":"1","m_id":1,"value":"Male"},
{"id":"2","m_id":1,"value":"Female"},
{"id":"3","m_id":1,"value":"Other"},
{"id":"47","m_id":10,"value":"Father"},
{"id":"48","m_id":10,"value":"Mother"},
{"id":"61","m_id":10,"value":"Cousin"},
{"id":"62","m_id":10,"value":"Other"}]

预期输出:

[{"id":"1","m_id":1,"value":"Male"},
{"id":"2","m_id":1,"value":"Female"},
{"id":"3","m_id":1,"value":"Other"}]

Click here

最佳答案

您也可以通过使用Javascript Filter()来做到这一点:

var result = obj1.filter(function(v){
return v.m_id == 1;
});

DEMO


AngularJS v.1.1.3 中,精确过滤器是 provided natively

var result = $filter('filter')(obj1, 
{m_id : 1},
true // ==========> this is for exact match
);

Working DEMO

关于angularjs - Angularjs Controller 中的过滤器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36472416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com