jsではどのようにhashオブジェクトのキーのペアを取得しますか?
1179 ワード
var obj = { '1' : 'apple', '2' : 'orange', '3' : 'pear' };
var arrMsg = ['apple', 'orange', 'pear'];
var array = [{'111': 'apple'}, {'222': 'orange'}];
for(var key in obj){
console.log(key); // 1 2 3
console.log(obj[key]); // apple orange pear
}
$.each(obj, function(i) {
alert(i); // 1 2 3
alert(obj[i]); // apple orange pear
});
for(var [key,value] in obj){
console.log(key); // 1 2 3
console.log(value); // undefined undefined undefined
}
// ,
for(var (key,value) in obj){
console.log(key); // 1 2 3
console.log(value); // apple orange pear
}
for(var val in arrMsg){
console.log(val); // 0 1 2
}
for(var val in arrMsg){
console.log(arrMsg[val]); // apple orange pear
}
for(var val in arrMsg){
console.log(val); // apple orange pear
}
原文に誤りがあり,少し改めます.https://blog.csdn.net/u013746071/article/details/80004475