一次迭代并访问多个地图(JavaScript)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一次迭代并访问多个地图(JavaScript)相关的知识,希望对你有一定的参考价值。

我有各种各样的地图具有相同的键,但不同的值 - 我试图计算沿地图的每个维度的平均值。例如,我可能有三个有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循环,迭代选定的地图并对所选地图的每个值执行平均等式并将其存储在具有相同键的新地图中?

答案

要获得每个键的平均值,您可以先将所有映射放在一个数组中。这将使迭代它们成为可能。然后获取其中一个映射的所有键的列表并迭代它。这假设所有地图都具有相同的键。

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)
另一答案

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

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)

})
另一答案

使用vanilla 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 );
}

以上是关于一次迭代并访问多个地图(JavaScript)的主要内容,如果未能解决你的问题,请参考以下文章

Javascript/Google 地图,自动创建的组合框在第二次迭代中失败

JavaScript迭代

javascript 选择并迭代多个选择器jquery中的每个元素

噪声小分队第一次迭代心得

Javascript处理对象数组

Javascript处理对象数组