通过筛选器在对象中查找项

Find an item in object by filter

本文关键字:查找 对象 筛选      更新时间:2023-09-26

我有以下对象:

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);
演示