Reputation: 15643
I have the following object:
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"
}
]
}
I'm trying to find a specific item (i.e item with id = 2
) and I'm using filter in javascript:
showMessage = function(msg_id) {
var found = $filter('filter')(lstMsg.results, {
id: msg_id
}, true);
result = found[0];
message = result.body;
title = result.title;
}
But it always returns the first item, no matter which id
I'm looking for.
I'm wondering where am I doing wrong?
Upvotes: 1
Views: 108
Reputation: 1580
Just with javascripttry this:
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);
Upvotes: 0
Reputation: 1520
You can use a simple for
loop and then filter on the id. not knowing what your trying to do after the filter specifically, I gave two examples of appending data to and innerHTML
elements or push
-ing to append to an array outside the loop.
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);
}
}
}
Upvotes: 0
Reputation: 5571
In Javascript you can filter in array by using filter function in arrays.
In Angularjs's function inside a controller.
$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;
});
};
The function above will return an array of an object that matches with the filter function.
By using:
$scope.found = $scope.search(msg_id)[0]; // $scope.found is the object properly.
I've made a first demo using plain 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]);
})();
Update: Using AngularJS:
Demo using filter in Javascript in AngularJS.
Final Update: Using $filter in AngularJS.
$filter("filter")($scope.lstMsg.results, {id: msg_id}); // Remove the boolean parameter.
Then:
$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;
};
In this demo I've implemented the filterAngular function. Basically, you need to remove the «true» parameter in your function.
Demo using $filter in Angular's controller
Upvotes: 1