如何按索引而不是名称映射JSON属性

时间:2018-11-15 06:55:56

标签: javascript jquery arrays json typescript

我有一个Web应用程序,我具有以下JSON数组:

var data=[ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
];

我想在一个单一的表中获取hits字段,例如 var hits = [5,1,1,1,1,1,1,6,1]

我了解data.map(x=>x.hits) 但是我在json中的属性名称是动态的。我事先不知道我的财产名称。所以我需要在Json中使用属性索引。

我知道可以通过json循环使用

但是有没有办法在一条语句中获得所有hits而没有任何循环?

7 个答案:

答案 0 :(得分:1)

赞:

let prop = 'hits';

data.map(x => x[prop]);

prop重新分配为所需的属性名称。

答案 1 :(得分:1)

首先,您需要使用Object.keys获取密钥。然后只需放置一个Map函数即可完成您的任务。

var data=[ 
     { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
     { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
     { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
     { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
     { "category" : "Business", "hits" : 1, "bytes" : 2847 },
     { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
     { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
     { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
     { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
    ];
var KeyName=Object.keys(data[0])[1]
data.map(x => x[KeyName]);

答案 2 :(得分:0)

代码段是:

var data=[ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
];

var idx = 1; // key2

var key = Object.keys(data)[idx];
value = o[key]

console.log(key,value); // key2 value2

您还可以使用它:

    var data=[ 
     { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
     { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
     { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
     { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
     { "category" : "Business", "hits" : 1, "bytes" : 2847 },
     { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
     { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
     { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
     { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
    ];

for(var prop in data) {
  console.log(prop,data[prop]);  
}

答案 3 :(得分:0)

您可以有两种不同的方法。如果密钥是动态的,但是您通过变量知道要寻找的密钥,您可以使用:

const keyname = 'hits';
data.map(x => x[keyname])

其中keyname是您根据需要设置的变量。

如果由于某些原因它只能由于使用索引而工作,则由于以下公式,您必须从索引中获取键名:

const j = 0; // or increment j through a for loop
const index = 3; // the index of the key you need
let keyname = Object.keys(data[j])[index];
data.map(x => x[keyname]);

答案 4 :(得分:0)

您可以利用Objcet.keys()生成的键数组的位置:

var data = [ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
];

var res = data.map(item => {
  var k = Object.keys(item);
  return item[k[1]];
});
console.log(res);

答案 5 :(得分:0)

如果我正确理解了您的问题,则您不知道data数组中的对象是否具有属性hits。也许它的拼写有所不同。

这是处理这种情况的一种方式

var data=[ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "HITS" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hit" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "HIT" : 1, "bytes" : 7601 },
 { "category" : "Business", "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
];


data.forEach(function(obj){
  //first, get an array of all availble keys in current element
  var keys = Object.keys(obj);
  
  //now we loop through all obtained keys
  keys.forEach(function(key){
  
    // the following line will match HIT/HITS/hit/hits
    if(key.toUpperCase().indexOf("HIT")>-1) {
       console.log("found hits: " , obj[key]);
    }
  });

});

答案 6 :(得分:0)

如果我理解您的意思是正确的,则您的数据是一个数组,并且此数组包含一些值。但也许其中之一没有属性hits。因此,您不想使用map方法,因为结果可能包含undefined个值?

如果这是您的问题,则可以在映射后尝试使用filter方法。像这样:

    var arr = [
        {
            'hits': 1
        },
        {
            'hits': 2
        },
        {
            'foo': 'bar'
        },
        {
            'hits': 3
        },
        {
            'hits': 4
        }
    ];

    var hits = arr.map(x => x.hits).filter(x => x);

    console.log(hits);

映射后,结果应为:

  

[1、2,未定义,3、4]

然后,我们需要将其过滤为:

  

[1、2、3、4]