簡體   English   中英

通過鍵值進行javascript關聯數組訪問

[英]javascript associative array access by key value

我有一個像這樣的數組:

employees = [
        {
        "id": 1,
        "shift_id": 1,
        "days": {
            "2012-03-01": 1,
            "2012-03-02": 1,
            "2012-03-03": 1,
            "2012-03-04": 0,
            "2012-03-05": 0,
            "2012-03-06": 0
        }},
    {
        "id": 2,
        "shift_id": 1,
        "days": {
            "2012-03-01": 0,
            "2012-03-02": 1,
            "2012-03-03": 1,
            "2012-03-04": 1,
            "2012-03-05": 1,
            "2012-03-06": 0
        }},
    {
        "id": 3,
        "shift_id": 2,
        "days": {
            "2012-03-01": 0,
            "2012-03-02": 0,
            "2012-03-03": 1,
            "2012-03-04": 1,
            "2012-03-05": 1,
            "2012-03-06": 1
        }}

    ];

有沒有一種方法可以使用id值訪問此數組中的元素? 也許在jQuery的東西? $(employees('id = 1');

只需遍歷數組並檢查id:

var yourId = 1;
for (var i = 0, len = employees.length; i < len; i++) {
  if (employees[i].id == yourId) {
     // ...
  }
}

您可以使用像這樣的函數,該函數適當地過濾數組:

var getEmployee = function (id) {
  return employees.filter(function(i) { return i.id == id; });
};

您可以使用此處記錄的.grep()方法:

var employee = $.grep(employees, function(e) { return e.id == 1 })[0];

嗯,有一種jQuery的方法:

var findElementById = function(elements, id) {
  return $.grep(elements, function(e) { return e.id === id; })[0];
}

還是我不知道為什么不只按id索引源數組。

或這個:

$.each(employee, function(){
    if(this["id"] == 2){
        console.log(this);
    }
});

也許您正在尋找類似以下的內容:

$.grep(employees, function(n){return n.id==1});

據我所知,要實現這一點,您必須遍歷它們

 Array.prototype.getObjectById = function(x){
      var catcher = false, i = 0;
      while(!catcher){
        catcher = this[i].id == x ? this[i] : false;
        i++;
      }
      return catcher;
    }

此功能應有幫助。 它將擴展數組對象,因此您可以將其用作myArray.getObjectbyId(id);

通過設計,這將返回符合條件的第一個對象。 您可以像這樣擴展它:

Array.prototype.getObjectsById = function(x){
  var catcher = [], i = 0;
  for(var i = 0; i < this.length; i++){
    if(this[i].id == value){
      catcher.push(this[i]);
    }
    i++;
  }
  return catcher.length == 1 ? catcher[0] : catcher;
}

如果多個對象符合條件,將返回對象數組。

 Array.prototype.getObjectsByAttribute = function(x, criteria){
      if(!criteria){criteria = 'id';}
      var catcher = [], i = 0;
      for(var i = 0; i < this.length; i++){
        if(this[i].criteria == value){
         catcher.push(this[i]);
        }
        i++;
      }
      return catcher.length == 1 ? catcher[0] : catcher;
    }

這進一步擴展了它以尋找任何標准。

我知道這個問題很舊,但是如果有人偶然發現這個問題,以供將來參考...

與其嘗試過度設計用於解析/檢查JSON的函數,不如考慮更改數據的結構以適合其用途。

考慮問題中的示例:

 data =  [ {
    "id": 1,
    "shift_id": 1,
    "days": {
        "2012-03-01": 1,
        "2012-03-02": 1,
        "2012-03-03": 1,
        "2012-03-04": 0,
        "2012-03-05": 0,
        "2012-03-06": 0
    }}, { .. }, {...} ]

以這種方式構造數據只能使您可以順序訪問對象,而無法通過特定索引查找對象。 在這種情況下,數組索引通常是沒有意義的。

data[0] => { id : 1, .. }
data[1] => { id : 2, .. }

如果id是非順序的或字母數字的,該怎么辦?

使用數組不會幫助您更快地搜索,但仍然需要循環...

而是考慮使用哈希表/對象:

{
'id1' =>  { id : 1 , data : { ... } }, 
'id99' => { id : 99, data : { ... } },
'id2' =>  { id : 2 , data : { ... } },
}

您可以將字符串值用作鍵,並通過執行以下操作直接訪問數據:

 data['id2'] => { id : 2, ... }

這將使您直接訪問要查找的數據(按ID)。 通過簡單地重新組織數據的結構,我們就可以從O(n)搜索變為O(1)搜索。

請記住,此方法對於某些解決方案可能比其他解決方案更好,並且還有許多其他注意事項。

這只是當您要通過唯一屬性查找數據時如何解決問題的一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM