如果我不知道密钥名称,如何在javascript中迭代键/值对

时间:2016-04-28 04:53:52

标签: javascript jquery json

我有一个json喜欢:

var json = [{"Google":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Linkedin":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Uber":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Akamai":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Apple":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
                {"Hulu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"IBM":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Spotify":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Amazon":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];

我想迭代它。 json.length返回9 =>暗示在这个json中有9个键/值对。如何在javascript中找到索引0处的键及其对应的值。

我尝试使用类似的东西:

$.each(json, function(k, v) {
        //display the key and value pair
        alert(k + ' is ' + v);
    });

但是这里的键返回1,2,3 ...而值为[object object]

6 个答案:

答案 0 :(得分:3)

  

使用Array.prototype.forEach()遍历迭代项目的arrayObject.keys,以获取单个对象的所有keys

var json = [{
  "Google": [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Linkedin": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Uber": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Akamai": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Apple": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Hulu": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "IBM": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Spotify": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
  "Amazon": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}];
json.forEach(function(item) {
  var keys = Object.keys(item);
  console.log(keys);
  //to get the values
  keys.forEach(function(key) {
    console.log(item[key]);
  })
});

答案 1 :(得分:1)

  

如何在索引0及其对应值中找到键   的JavaScript。

您可以使用Object.keys方法获取对象中的所有键名。

var key0 = Object.keys(json[0])[0];

这将在索引0处为您提供密钥名称。

并且

value0 = json[0][key0];

这可以更通用,以遍历整个json

json.forEach(function(val){
   var key = Object.keys(val)[0];
   var value = val[key];
   console.log( "key is " + key );
   console.log( "value is " + value );
});

答案 2 :(得分:1)

您应该考虑重新格式化JSON数据。

我个人认为,给每个价值一个合适的属性"是个好习惯。使用JSON对象时(Giving" Google"属性为#34;公司"),以便更轻松地访问价值观&可读性。

EG:

var json = [{'company':"Google",'values':[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"Linkedin",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"Uber",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"Akamai",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"Apple",'values':[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
            {'company':"Hulu",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"IBM",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"Spotify",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
            {'company':"Amazon",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];

然后,您可以使用2 $ .each循环遍历值。一个嵌套在另一个之后。

$.each(json,function(key,val){

   var company = val.company;
   console.log('Below values belong to '+company);

   $.each(val.values,function(k,v){
     var value = v;
     console.log(value);
   });

   console.log('End of values belonging to '+company);
});

要在索引0处找到密钥(我假设按键表示公司名称),您可以访问索引为0的json数组。

var company = json[0].company;
var valueArray = json[0].values;

答案 3 :(得分:1)

类似这样的事情

autoptimize_9e42ecb6dfd481fe6fed38efb1dbf60d.css

答案 4 :(得分:1)

希望这会有用

var json = [
                {"Google":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Linkedin":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Uber":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Akamai":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Apple":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
                {"Hulu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"IBM":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Spotify":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
                {"Amazon":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
             ];

     json.forEach(function(key,value){
     var _getCurrentItem = key; // will give google:Array[18]....
     // Loop through this object to get its key & value
         for(var keys in _getCurrentItem){ 
         console.log('Key is ' + keys +' & its value' +_getCurrentItem[keys]);
     }) 

检查此Jsfiddle是否有工作模式

答案 5 :(得分:1)

[]表示一个数组,表示键是整数:0到(N-1):N是数组/列表中的项数。即:var t = ["Me", "You", "Them"];其中t[0] is "Me"t[2] is "Them"

{}表示对象或关联数组:带有字符串键的数组。即var t = {"firstperson" : "me", "secondperson" : "you", "thirdperson" : "them"};其中t["firstperson"] is "me"

因此,在您的数据表示中,您应该有两个循环,一个在另一个循环中:

$.each(json, function(k, v) {
    //display the key and value pair
    $.each(v, function(k2, v2) {
            //display the key and value pair
            alert('json[' + k + '][' + k2 + '] = ' + v2);
        });
});