Javascript-计算重复的JSON值并将计数与关联键一起排序?
可能重复:Javascript-计算重复的JSON值并将计数与关联键一起排序?,javascript,json,object,loops,associative-array,Javascript,Json,Object,Loops,Associative Array,可能重复: 我希望获得JSON中某些值的最佳结果。通过示例更容易解释: var jsonData = { bom: [ { "Component":"Some Thing", "Version":"Version ABC", "License":"License ABC", }, { "Component":"Another Thing",
我希望获得JSON中某些值的最佳结果。通过示例更容易解释:
var jsonData = {
bom: [
{
"Component":"Some Thing",
"Version":"Version ABC",
"License":"License ABC",
},
{
"Component":"Another Thing",
"Version":"Version XYZ",
"License":"License ABC",
},
etc ....
]
}
因此,我的目标是确定“许可证ABC”或其他许可证的出现次数为X,然后我希望能够对要插入DOM的key:val对进行排序,作为“前X个最流行的许可证是:
- 许可证ABC-100
- XYZ-70许可证
- 许可证123-25
var countResults = function() {
var fileLicenses = [];
for ( var i = 0, arrLen = jsonData.files.length; i < arrLen; ++i ) {
fileLicenses.push(jsonData.files[i]["License"]);
}
keyCount = {};
for(i = 0; i < fileLicenses.length; ++i) {
if(!keyCount[fileLicenses[i]]) {
keyCount[fileLicenses[i]] = 0;
}
++keyCount[fileLicenses[i]];
}
console.log( keyCount );
}();
但我不知道该怎么做。我只需要对数字右边的列进行排序,并让相关的键保持不变。想法?谢谢!你不能根据对象的值对其进行排序。你可以做的是将其转换为一个对象数组,然后进行排序。类似于:
var rank = function(items, prop) {
//declare a key->count table
var results = {}
//loop through all the items we were given to rank
for(var i=0;len=items.length;i<len;i++) {
//get the requested property value (example: License)
var value = items[i][prop];
//increment counter for this value (starting at 1)
var count = (results[value] || 0) + 1;
results[value] = count;
}
var ranked = []
//loop through all the keys in the results object
for(var key in results) {
//here we check that the results object *actually* has
//the key. because of prototypal inheritance in javascript there's
//a chance that someone has modified the Object class prototype
//with some extra properties. We don't want to include them in the
//ranking, so we check the object has it's *own* property.
if(results.hasOwnProperty(key)) {
//add an object that looks like {value:"License ABC", count: 2}
//to the output array
ranked.push({value:key, count:results[key]});
}
}
//sort by count descending
return ranked.sort(function(a, b) { return b.count - a.count; });
}
/代码未测试谢谢fencliff!插入它,效果很好。仍然需要一些时间来理解逻辑,但正是我所需要的。谢谢!@Matt我在代码中添加了一些注释来解释逻辑。如果解决方案有效,不要忘了将答案标记为已接受!哇!非常感谢fencliff的注释。解决方案有效从一开始就很好,但是,理解你正在插入的代码总是更好!这很有帮助。再次感谢!@Matt,不客气。我不会使用在互联网上找到的任何代码,除非我理解:)
var rank = function(items, prop) {
//declare a key->count table
var results = {}
//loop through all the items we were given to rank
for(var i=0;len=items.length;i<len;i++) {
//get the requested property value (example: License)
var value = items[i][prop];
//increment counter for this value (starting at 1)
var count = (results[value] || 0) + 1;
results[value] = count;
}
var ranked = []
//loop through all the keys in the results object
for(var key in results) {
//here we check that the results object *actually* has
//the key. because of prototypal inheritance in javascript there's
//a chance that someone has modified the Object class prototype
//with some extra properties. We don't want to include them in the
//ranking, so we check the object has it's *own* property.
if(results.hasOwnProperty(key)) {
//add an object that looks like {value:"License ABC", count: 2}
//to the output array
ranked.push({value:key, count:results[key]});
}
}
//sort by count descending
return ranked.sort(function(a, b) { return b.count - a.count; });
}
var sorted = rank(jsonData.bom, "License");
var first = sorted[0].value;