从数组中的键/对对象获取键和值 [英] Get key and value from key/pair objects in an array

查看:502
本文介绍了从数组中的键/对对象获取键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个数组



So I have this array

var array = [ 

     { '1': '3PwGtv534oy9RTeWAAAB' }, 
     { '2': '_tKGtyOU6OtU6tdgPOGG' }, 
     { '3': '86JGHnjR3UJ88An09M32' } 
]





我想要检索每个对象的键和值。



我尝试过: < br $>




I want to retrieve both the key and the values of each object.

What I have tried:

for(var i in array){
   console.log(i);
   console.log(array[i]);
}





但它在



But it gives the object index in

console.log(i); 

console.log(array[i]);





我也尝试过循环两次,但它也不起作用。



I also tried looping twice but it doesn't work either.

for(var i in array){
   for(var j in i){
     console.log(j);
     console.log(i[j]);
   }
}

推荐答案

这是一个使用jQuery的工作示例:cp_json_get_key_value - JSFiddle [ ^ ]
Here is a working example using jQuery: cp_json_get_key_value - JSFiddle[^]


这里是 javascript 版本

here is the javascript version
var array = [

    { '1': '3PwGtv534oy9RTeWAAAB' },
    { '2': '_tKGtyOU6OtU6tdgPOGG' },
    { '3': '86JGHnjR3UJ88An09M32' }
       ];
       for (var i = 0; i < array.length; i++) {
           var item = array[i];
           for (obj in item) {
               console.log(obj, item[obj]);
           }
       }


这篇关于从数组中的键/对对象获取键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆