2013-10-26 102 views

回答

2

在jQuery中:

var myObject = { "the name": "the value", "the other name": "the other value" }; 
var indexes = []; 
$.each(myObject,function(index,value){ 
    indexes.push(index); 
}); 

console.log(indexes[0]) -> the name 
console.log(indexes[1]) -> the other name 

在純JS:

var myObject = { "the name": "the value", "the other name": "the other value" }; 
var indexes = []; 
for(var index in myObject){ 
    indexes.push(index); 
} 

console.log(indexes[0]) -> the name 
console.log(indexes[1]) -> the other name 

在上面,你可以打破,只要你想。如果你需要的所有指標,還有就是擺陣更快的方法:我不是一定要完成什麼

var myObject = { "the name": "the value", "the other name": "the other value" }; 
var indexes = Object.keys(myObject); 

console.log(indexes[0]) -> the name 
console.log(indexes[1]) -> the other name 
1

當然。您可以使用for-in循環來獲取對象的屬性名稱。

for (var prop in myObject){ 
    console.log("Property name is: " + prop + " and value is " + myObject[prop]); 
} 

More info on for-in here

1

,但你可以 對象拿到鑰匙的對象很容易地。鍵(對象)

Object.keys(myObject) 

這將給你一個對象的鍵的數組,你可以做任何你想做的事情。

1

大多數現代瀏覽器都會讓您使用Object.keys方法從JSON對象中獲取鍵列表(這就是您要查找的字符串)。所以,你可以簡單地使用

var keys = Object.keys(myJsonObject); 

得到一個數組的鍵和做你想這些。