通过筛选器在对象中查找项
Find an item in object by filter
我有以下对象:
lstMsg = {
"count": 6,
"next": null,
"previous": null,
"results": [{
"id": 3,
"sender": 2,
"receiver": {
"id": 4,
"username": "ghazan",
"first_name": "ghazan",
"last_name": "ghazan"
},
"title": "salam",
"body": "reza khoobi",
"created_time": "20-6-1394 15:42:34.647251"
},
{
"id": 2,
"sender": 2,
"receiver": {
"id": 4,
"username": "ghazan",
"first_name": "ghazan",
"last_name": "ghazan"
},
"title": "reis",
"body": "salam reis",
"created_time": "20-6-1394 15:41:49.512305"
},
{
"id": 1,
"sender": 2,
"receiver": {
"id": 4,
"username": "ghazan",
"first_name": "ghazan",
"last_name": "ghazan"
},
"title": "shaftan",
"body": "saalam",
"created_time": "20-6-1394 15:41:38.626508"
}
]
}
我试图找到一个特定的项目(即项目与id = 2
),我在javascript中使用过滤器:
showMessage = function(msg_id) {
var found = $filter('filter')(lstMsg.results, {
id: msg_id
}, true);
result = found[0];
message = result.body;
title = result.title;
}
但是它总是返回第一项,不管我在寻找哪个id
。
我想知道我做错了什么?
在Javascript中,你可以在数组中使用filter函数来过滤。
在Angularjs的控制器内部的函数中
$scope.lstMsg = [{},{},{}]; // Array of objects.
$scope.search = function (value) {
return $scope.lstMsg.results.filter(function (e) { // Where e is equal an object of $scope.lstMsg array.
return e.id == value;
});
};
上面的函数将返回一个与过滤器函数匹配的对象数组。
通过使用:$scope.found = $scope.search(msg_id)[0]; // $scope.found is the object properly.
我用纯javascript做了第一个演示。
(function() {
var lstMsg = {
"count": 6,
"next": null,
"previous": null,
"results": [{
"id": 3,
"sender": 2,
"receiver": {
"id": 4,
"username": "ghazan",
"first_name": "ghazan",
"last_name": "ghazan"
},
"title": "salam",
"body": "reza khoobi",
"created_time": "20-6-1394 15:42:34.647251"
}, {
"id": 2,
"sender": 2,
"receiver": {
"id": 4,
"username": "ghazan",
"first_name": "ghazan",
"last_name": "ghazan"
},
"title": "reis",
"body": "salam reis",
"created_time": "20-6-1394 15:41:49.512305"
}, {
"id": 1,
"sender": 2,
"receiver": {
"id": 4,
"username": "ghazan",
"first_name": "ghazan",
"last_name": "ghazan"
},
"title": "shaftan",
"body": "saalam",
"created_time": "20-6-1394 15:41:38.626508"
}]
};
function search(value) {
return lstMsg.results.filter(function(e) {
return e.id == value
});
}
console.log(search(2)[0]);
})();
更新:Using AngularJS:
在AngularJS中演示使用Javascript过滤器。
最终更新:在AngularJS中使用$filter .
$filter("filter")($scope.lstMsg.results, {id: msg_id}); // Remove the boolean parameter.
:
$scope.filterAngular = function (msg_id) {
$scope.found = $filter("filter")($scope.lstMsg.results, {id: msg_id})[0];
console.log($scope.found);
$scope.message = $scope.found.body;
$scope.title = $scope.found.title;
};
在这个演示中,我实现了filterAngular函数。基本上,您需要删除函数中的«true»参数。
在Angular控制器中使用$filter的演示
您可以使用简单的for
循环,然后对id进行过滤。不知道在过滤器之后要做什么,我给出了两个例子,将数据附加到和innerHTML
元素或将push
附加到循环外的数组。
function showMessage() {
var p1 = document.getElementById("result_body");
var p2 = document.getElementById("result_title");
for (var i = 0; i < lstMsg.results.length; i++) {
if (lstMsg.results[i].id === 2) {
//SIMPLE OUTPUT FOR DEMO
p1.innerHTML = lstMsg.results[i].body;
p2.innerHTML = lstMsg.results[i].title;
//COULD USE AN PUSH TO AN ARRAY
someArray.push(lstMsg.results[i].body);
someArray.push(lstMsg.results[i].title);
}
}
}
就用javascript试试这个:
Array.prototype.filterObjects = function(key, value) {
return this.filter(function(x) {
return x[key] === value;
})
}
var lt = lstMsg['results'];
var res = lt.filterObjects('id', 2);
演示相关文章:
- Javascript:根据对象的嵌套数组中的值,在数组中查找对象的索引
- 正在数组中查找对象
- 在prototypejs中查找对象的类
- 通过知道对象的id在JSON中查找对象
- Javascript在JSON中查找对象(我使用Angular)
- 查找对象数组是否包含其中一个标记的最快方法
- 如何将查找对象值传递到FetchXml查询中
- JavaScript-通过类似的属性查找对象,并将其推送到一个新的数组中
- 通过匹配 - Javascript/AngularJS 中的参数来查找对象
- 正在查找对象集合的最大值
- 根据Angular JS中的属性值在列表中查找对象
- 在下划线中的对象中查找对象中的重复项
- 如何使用正则表达式查找对象成员名称
- 通过包含的字符串查找对象
- 在 Firebase 中通过对象的唯一 id 查找对象
- 在 javascript 中查找对象的类型
- 查找对象树中特定字段值的所有出现
- 如何在javascript数组中按键和值查找对象的索引
- 使用 Javascript/React.js 查找对象的数组索引
- 查找对象数组(Angularjs)中是否存在值