一次遍历并访问多个地图(JavaScript)

时间:2018-09-07 16:49:03

标签: javascript maps

我有各种各样的地图,这些地图具有相同的键,但是值不同-我正在尝试沿着地图的各个维度计算平均值。例如,我可能有三个具有3个键的地图-这个数字以及键的数目可能会改变。

var a = new Map ([["wordOne", 153], ["wordTwo", 24], ["wordThree", 73]]);
var b = new Map ([["wordOne", 346], ["wordTwo", 693], ["wordThree", 124]]);
var c = new Map ([["wordOne", 34], ["wordTwo", 4352], ["wordThree", 93]]);

我正在尝试复制下面的等式,所以我可以从我选择的3张不同的地图中获得该键的平均值

[(a_1 + b_1 + c_1)/3]
[(a_2 + b_2 + c_2)/3]
[(a_3 + b_3 + c_3)/3]

然后,我想取平均值并将其连同平均值表示的键一起存储在新地图中。

我如何在JavaScript中创建一个for循环,该循环遍历选择的地图,并对所选地图的每个值执行平均方程式,然后使用相同的键将其存储在新地图中?

3 个答案:

答案 0 :(得分:0)

要获取每个键的平均值,您可以先将所有地图放在一个数组中。这将使遍历它们成为可能。然后获取其中一张地图的所有键的列表,然后对其进行迭代。假定所有地图都具有相同的键。

var a = new Map ([["wordOne", 153], ["wordTwo", 24], ["wordThree", 73]]);
var b = new Map ([["wordOne", 346], ["wordTwo", 693], ["wordThree", 124]]);
var c = new Map ([["wordOne", 34], ["wordTwo", 4352], ["wordThree", 93]]);
// make array of maps
let maps = [a, b, c];

// Average of each key across maps (i.e [average_a, average_b, average_c])
// map over all keys(), then average across the maps
let averages_across = [...maps[0].keys()].map(key => [key, maps.reduce((sum, map) => sum + map.get(key), 0)/maps.length])

console.log(averages_across)
// make a new map from this
let average_map = new Map(averages_across)

答案 1 :(得分:0)

我不确定是否理解,但是每次迭代访问每个键的每个值可能是其中之一:

var mapOne = new Map ([["a", 153], ["b", 24], ["c", 73]]);
var mapTwo = new Map ([["a", 346], ["b", 693], ["c", 124]]);
var mapThree = new Map ([["a", 34], ["b", 4352], ["c", 93]]);

//each key of all maps in each iteration. a+a+a/3 b+b+b/3... etc
mapThree.forEach((v,k)=>{

    console.log((mapOne.get(k)+mapTwo.get(k)+v)/3)

});


//the average of each map
[...[mapOne],...[mapTwo],...[mapThree]].forEach(m=>{

    var res = 0;

    mapThree.forEach((v,k)=>{// a+b+c/3 ... etc
    
        res += mapOne.get(k);
    
    })
    
    console.log(res/3)

})

答案 2 :(得分:0)

使用普通的JavaScript和Map方法。

var mapOne = new Map ([ ["a", 153], ["b", 24], ["c", 73] ]);
var mapTwo = new Map ([ ["a", 346], ["b", 693], ["c", 124] ]);
var mapThree = new Map ([ ["a", 34], ["b", 4352], ["c", 93] ]);

function averageValuesKeyMaps ( ...maps )
{
  // Map for key => averages
  const averagesMap = new Map ();

  let 
    // Get the first map as a reference,
    mapRef = maps[ 0 ],
    // Get the size, assuming that each map is the same size
    size = mapRef.size,
    // Get the keys, assuming that every maps have those keys
    keys = maps[ 0 ].keys()

  // For each key in keys 
  for ( let key of keys )
  {
    // Get the average with each value of the current key for each map
    let average = maps.reduce( ( acc, map ) => acc + map.get( key ), 0 ) / size;
    
    // Set the key => average
    averagesMap.set( key, average );
  }

  // Return the new map of averages
  return averagesMap;
}

// Get the map of averages key values
let averagesMap = averageValuesKeyMaps( mapOne, mapTwo, mapThree );

// Usage example
for ( let [ key, average ] of averagesMap.entries() )
{ 
  // Do something with key or average
  console.log( key, average );
}