2016-11-08 30 views
3

這是我的數據對象

{ 
    "teachers":{ 
     "0":{ 
      "info":{ 
       "id":"1", 
       "age":"22", 
       "driverlicense":"yes" 
      } 
     }, 
     "1":{ 
      "info":{ 
       "id":"2", 
       "age":"51", 
       "driverlicense":"yes" 
      } 
     }, 
     "2":{ 
      "info":{ 
       "id":"1", 
       "age":"25", 
       "driverlicense":"no" 
      } 
     } 
    } 
} 

我儘量讀的是info對象,並從那裏的信息。

我的代碼

$.each(data.teachers, function(item) { 
    if(item.info.age < '25'){ 
     // stores in array 
     yTeachers[item.info.id] = item.info.age; 
    } 
}); 

但它給了我undefined回來。

如果$.each完成,是否可以讀取數組teachers?有時當我運行另一個功能時它仍在運行。也許使用類似promise的東西?

+0

教師不是數組,是與鍵 「0」 的對象, 「1」,等等。你將不得不使用用於(在數據密鑰。老師) – yBrodsky

+0

@KevinB,對不起..我編輯了這篇文章。 –

+0

@ user1655756幸運的是$ .each不支持對象,所以如果使用得當,操作可以繼續使用$ .each。 –

回答

1

使用此參考檢查fiddle

var data = { 
    "teachers": { 
     "0": { 
      "info": { 
       "id": "1", 
       "age": "22", 
       "driverlicense": "yes" 
      } 
     }, 
     "1": { 
      "info": { 
       "id": "2", 
       "age": "51", 
       "driverlicense": "yes" 
      } 
     }, 
     "2": { 
      "info": { 
       "id": "1", 
       "age": "25", 
       "driverlicense": "no" 
      } 
     } 
    } 
}; 



    var yTeachers=[]; 
    $.each(data.teachers, function (i, item) { 
     if (item.info.age < 25) { 
      yTeachers[item.info.id] = item.info.age; 
     } 
    }); 
1

這應該爲你工作就yTeachers存儲ages > 25爲對象。

var json = { 
    "teachers": { 
     "0": { 
      "info": { 
       "id": "1", 
       "age": "22", 
       "driverlicense": "yes" 
      } 
     }, 
     "1": { 
      "info": { 
       "id": "2", 
       "age": "51", 
       "driverlicense": "yes" 
      } 
     }, 
     "2": { 
      "info": { 
       "id": "1", 
       "age": "25", 
       "driverlicense": "no" 
      } 
     } 
    } 
}; 

var yTeachers = []; 
$.each(json.teachers, function(index,item) { 
    var id = item.info.id; 
    var age = item.info.age; 

    if(age < 25){ 
     // push it in array 
     yTeachers.push({id:age}); 
    } 
}); 

console.log(yTeachers); 
+0

如何檢查'$ .each'完成了嗎?所以我可以調用另一個函數?我想根據年齡對數組進行排序,但是隻有當所有年齡段都在數組中時,我才能做到這一點 –

+0

@AlterEgo,$ .each()你可以簡單地在$ .each()後面運行代碼,例如'.each(obj,function(i,item){}); yourfunction();' –

1

等待$。每完成

無需等待$.each做後只需撥打您的變量。

$.each的回調函數有2個參數,第1項爲index/key,第2項爲value

  1. 與陣列檢查被稱爲最後一個項目,做使用index == array.length -1

    $.each(array, function(index, value){ 
    if(index == array.length -1){ 
        console("$.each is done and last item called"); 
        } 
    }); 
    
  2. 與對象,檢查調用,完成使用key == Object.keys(obj).pop()最後的關鍵,這是你的情況。

    $.each(obj, function(key, value){ 
    if(key == Object.keys(obj).pop()){ 
        console("$.each is done and last item called"); 
        } 
    }); 
    

var data = { 
 
    "teachers": { 
 
    "0": { 
 
     "info": { 
 
     "id": "1", 
 
     "age": "22", 
 
     "driverlicense": "yes" 
 
     } 
 
    }, 
 
    "1": { 
 
     "info": { 
 
     "id": "2", 
 
     "age": "51", 
 
     "driverlicense": "yes" 
 
     } 
 
    }, 
 
    "2": { 
 
     "info": { 
 
     "id": "1", 
 
     "age": "25", 
 
     "driverlicense": "no" 
 
     } 
 
    } 
 
    } 
 
} 
 
$.each(data.teachers, function(i, item) { 
 
    if (item.info.age < 25) { 
 
    alert(item.info.age); 
 
    } 
 
    if (i == Object.keys(data.teachers).pop()) { 
 
    console.log("Done") 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>