用于迭代过程的嵌套过滤-指数映射? [英] Nested filtering for iterative process -exponential mapping?

查看:105
本文介绍了用于迭代过程的嵌套过滤-指数映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们从简单的形式开始. 假设您有一个像这样的简单数据集:您想检索每种资产的累计金额.

首先,我将根据资产值过滤(排列)数组

let's begin with the simple form. Imagine you have a simple dataset like this one: you want to retrieve the cumulative amounts for each asset.

First I would filter (arrange) the array by the asset value

  var pairs   = ["eur", "usd", "pop", "dot", "cad", "sol"];
  for(i=0; i<pairs.length; i++){
  var filtArray1  = dataArray.filter(function filt(el){return el.asset === pairs[i];});  
    filtArrays.push(filtArray1);

然后执行任何类型的操作,例如对距离进行总结:

and then perform any sort of operations, like summing up for istance:

  var values  = Object.keys(filtArrays[i]).map(function(e){return filtArrays[i][e].amount});
  parziali.push(values);

  var somma   = values.reduce(function(acc, val) { return acc + val; }, 0);
    somme.push(somma); 

//result "somme": [9.0, 9.0, 6.0, 6.0, 9.0, 3.0]
}

好的,您可以使用indexOf();或任何其他更快的方法,但这不是重点.

ok, you can use indexOf(); or any other faster method but that's not the point.

重点是:假设我们现在在此数据集的 date字段上添加一个额外的层.现在我们有:

并且,与以前一样,您希望能够检索每个周的每个资产的金额(这些是一年中的周数)..
你是怎么做到的?
它突然变成了指数.您可以使用哪种类型的流程来保持迭代(即:自动)并同时减轻工作量?

The point is: imagine we add an extra layer to this data set now, some date field. Now we have:

and, as for before, you want to be able to retrieve the amount for each asset for each week(those are week numbers of the year)..
How do you do that?
It has suddenly become exponential. What type of process can you use to keep it iterative (i.e.: automatic) and light on work-load at the same time?

您甚至可能要在此时添加额外的数据层..

如您所见,我们有两个不同的钱包,两个都装有欧元,但都在同一周..
现在,我们希望能够在每个每个钱包中检索每个资产的数量.就像我说的那样,它变得指数:您如何处理?谢谢

You could even want to add an additional layer of data at this point..

as you can see we have two different wallets, both holding euros, but in the same week.
Now we want to be able to retrieve the amount of each asset for each week AND for each wallet. As I said, it kind of becomes exponential: how do you approach that? thanks

ps:数据显然以前用

ps: the data was obviously previously treated with

  for(i=0; i<data.length; i++){
    var dataRow = data[i];
    var record = {};
    record['weeks']    = dataRow[0];
    record['asset']    = dataRow[1];
    record['amount']   = dataRow[2];
    dataArray.push(record);}  

推荐答案

据我了解,该问题不是指数级的.复杂度仍然是线性的,只是在扫描可用数据时简单地添加更多标准.

The problem, as I understand it, is not exponential. The complexity is still linear, one is simply adding more criteria when scanning through the available data.

该技术是根据您要分组以实现小计的字段累积数据.例如,如果要按年/月分组,则只需定义年/月桶的年/月.例如,您将具有"20181","20182","20183"等的存储桶.当您遍历数组中的条目时,将使用年/月来标识存储桶并将条目值添加到其中该小桶.

The technique is to accumulate the data based on the fields that you're trying to group to achieve the subtotals. For example, if you want to group by year/month, then all that is needed is the year/month to define the subtotal buckets. Eg, you will have a bucket for '20181', '20182', '20183', etc. As you loop through the entries in the array, you will use the year/month to identify the bucket and to add the entries value into that subtotal bucket.

如果要在小计分组中包括其他字段(例如,货币和年/月),则只需调整存储桶名称以同时包含货币和年/月即可.即,您的小计水桶现在将是〜cad〜20181〜",〜cad〜20182〜",〜eur〜20181〜"等.这将按货币和年/月唯一地标识小计水桶.然后,像以前一样,当遍历数组中的条目时,您将从数组条目中获取值以标识该值所属的货币和年/月存储区.请注意,代字号是用于分隔字段的任意定界符构造小计存储桶名称时的值.

If you are including additional fields as part of the subtotal grouping (eg, currency and year/month), then it is simply a matter of adjusting the bucket names to include both currency and year/month. Ie, your subtotal buckets will now be '~cad~20181~', '~cad~20182~', '~eur~20181~', etc. This uniquely identifies the subtotal buckets by currency and year/month. Then, as before, when looping through the entries in the array, you are taking the values from the array entry to identify the currency and year/month bucket that the value belongs... Note that the tildes are arbitrary delimiters to separate the field values when constructing the subtotal bucket names.

a = [
  {geo:"cad", ym:20182, value:3},
  {geo:"eur", ym:20181, value:1},
  {geo:"pop", ym:20182, value:2},
  {geo:"usd", ym:20181, value:3},
  {geo:"cad", ym:20182, value:3},
  {geo:"sol", ym:20181, value:1},
  {geo:"cad", ym:20181, value:3},
  {geo:"pop", ym:20182, value:2},
  {geo:"pop", ym:20181, value:5}
];

 var result = a.reduce( (totals, entry) => {
   let key = '~' + entry.geo + '~' + entry.ym + '~';
   totals[key] = ( totals[key] || 0 ) + entry.value;
   return totals;
 }, {} );


console.log( result );

使用for循环更改代码.

arr = [
  {geo:"cad", ym:20182, value:3},
  {geo:"eur", ym:20181, value:1},
  {geo:"pop", ym:20182, value:2},
  {geo:"usd", ym:20181, value:3},
  {geo:"cad", ym:20182, value:3},
  {geo:"sol", ym:20181, value:1},
  {geo:"cad", ym:20181, value:3},
  {geo:"pop", ym:20182, value:2},
  {geo:"pop", ym:20181, value:5}
];

result = {};

// Loop through each entry in arr

for ( let i = 0; i < arr.length; i++ ) {
  
  // Create the subtotal bucket name based on the fields
  // defining the grouping.

  let key = '~' + arr[ i ].geo + '~' + arr[ i ].ym + '~';
  
  // Now that we know which subtotal bucket arr[i].value
  // belongs, let's add it to the bucket.
  result[ key ] = (result[ key ] || 0 ) + arr[ i ].value ;

}

console.log( result );

在两个示例中,我都将值收集到一个对象中,每个属性代表一个小计的存储桶.也可以使用Map对象,但是在举例说明根据小计将如何分组小计桶的关键概念时,不清楚.

In both examples, I've collected the values into an object with each property representing a subtotal bucket. A Map object can be used as well, but is less clear in exemplifying the critical concept of naming the subtotal buckets according to how the subtotals are to be grouped...

希望这会有所帮助.

这篇关于用于迭代过程的嵌套过滤-指数映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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