[英]How to count a huge list of items
I have a huge list of items about almost all the crops and these data is to be plotted using maps and charts.我有一个关于几乎所有作物的大量项目列表,这些数据将使用地图和图表绘制。 I would like to count the number of each crop, say how many times was cabbage planted.
我想计算每种作物的数量,说白菜种植了多少次。 I use Firebase database to store the data and I retrieve it using this function below:
我使用 Firebase 数据库存储数据,并使用下面的 function 检索它:
database = firebase.database()
var ref = database.ref('Planting-Calendar-Entries');
ref.on('value', gotData, errData);
function gotData(data){
console.log(data.val())
var veggie = data.val();
var keys = Object.keys(veggie);
console.log(keys);
let counter = 0
for (var i = 0; i < keys.length; i++){
var k = keys[i];
var Veg_planted = veggie[k].Veg_planted;
var coordinates = veggie[k].coordinates;
if (Veg_planted == 'Cabbage'){
counter++;
}
// vegAll = Veg_planted.count()
console.log(Veg_planted, coordinates)
}
console.log(counter)
}
function errData(err){
console.log('Error!');
console.log(err)
}
This data I retrieve it from the database where it gets updated whenever someone submits their planting information.我从数据库中检索这些数据,每当有人提交他们的种植信息时,它就会更新。 The code I used above will only apply if my list is small, but I have a list of about 170 items and it would be hard to write code to count each crop individually using something like
let counter = 0, counter++
.我上面使用的代码仅适用于我的列表很小的情况,但我有一个大约 170 个项目的列表,并且很难编写代码来使用
let counter = 0, counter++
之类的东西单独计算每个作物。 Is there a way I could navigate around this?有没有办法可以解决这个问题?
Actually: the code to count the items is probably going to be the same, no matter how many items there are.实际上:计算项目的代码可能是相同的,不管有多少项目。 The thing that is going to be a problem as you scale though is the amount of data that you have to retrieve that you're not displaying to the user.
但是,当您进行扩展时,将成为问题的是您必须检索的数据量,而这些数据量并未显示给用户。
Firebase does not support aggregation queries, and your approach only works for short lists of items. Firebase 不支持聚合查询,您的方法仅适用于项目的短列表。 For a more scalable solution, you should store the actual count itself in the database too.
对于更具可扩展性的解决方案,您也应该将实际计数本身存储在数据库中。
So:所以:
blaCount
property for each bla
that exists.bla
都有一个blaCount
属性。bla
to/from the database.bla
时,递增/递减计数器。I'm assuming data.val()
returns an array, not an object, and you're misusing Object.keys()
on an array instead of just looping over the array itself.我假设
data.val()
返回一个数组,而不是 object,并且您在数组上滥用Object.keys()
而不仅仅是循环数组本身。 If that's true, then it sounds like you want to group by the Veg_planted
key and count the groupings:如果这是真的,那么听起来你想按
Veg_planted
键分组并计算分组:
const counts = Object.values(veggie).reduce((counts, { Veg_planted }) => ({
...counts,
[Veg_planted]: (counts[Veg_planted] || 0) + 1
}), {});
Usage:用法:
const veggie = [{ Veg_planted: 'Cabbage' }, { Veg_planted: 'Cabbage' }, { Veg_planted: 'Corn' }];
// result of counts:
// {Cabbage: 2, Corn: 1}
var plantingRef = db.collection("PlantingCalendarEntries");
var query = plantingRef.where("Veg_planted", "==", "Cabbage");
if you still want to stuck with realtime database.如果您仍然想坚持使用实时数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.