Digamos que tenemos la siguiente matriz de diccionarios:
var dictionary_demo = [ [ {country: "georgia", value: sunny}, {country: "france", value: rainy} ], [ {country: "georgia", value: sunny}, {country: "france", value: gloomy} ], [ {country: "georgia", value: rainy}, {country: "france", value: dry} ] ]¿Cómo obtendría la salida que me dice:
en georgia: soleado ocurre 2 veces y lluvioso ocurre 1 vez.
En Francia: lluvioso ocurre 1 vez, sombrío ocurre 1 vez y seco ocurre 1 vez
Con una matriz de matrices (la terminología adecuada para un diccionario en JS es matriz), sería fácil usar flatMap para extraer todos los valores de las matrices internas a la matriz externa. Desde allí, puede iterar sobre la matriz plana y recopilar los valores que desee.
Para este ejemplo, podría usar el método array.reduce para transformar la matriz plana resultante en un objeto con una clave para cada país, y el valor es otro objeto con claves para cada tipo de clima y el valor de la frecuencia de ocurrencia.
var dictionary_demo = [ [ {country: "georgia", value: "sunny"}, {country: "france", value: "rainy"} ], [ {country: "georgia", value: "sunny"}, {country: "france", value: "gloomy"} ], [ {country: "georgia", value: "rainy"}, {country: "france", value: "dry"} ] ] function weatherFrequency(arr) { // flatten the array const flatArr = arr.flatMap(a => a) // use flattened array to transform the values into frequencies const sortedArr = flatArr.reduce((accum, { country, value }) => { // check if key exists, if not, add it if (!accum[country]) accum[country] = {} // check if weather type exists, if so add 1, if not, assign 1 accum[country][value] ? accum[country][value] += 1 : accum[country][value] = 1 // return the accumulator for the next iteraction of `reduce` return accum }, {}) return sortedArr } // check resulting object from the `reduce` function console.log(weatherFrequency(dictionary_demo)) // produce a string with the values you want const countryWeather = weatherFrequency(dictionary_demo) // use string interpolation to extract values you need console.log(`in georgia: sunny occurs ${countryWeather.georgia.sunny} times, and rainy occurs ${countryWeather.georgia.rainy} time`) var dictionary_demo = [ [ {country: "georgia", value: "sunny"}, {country: "france", value: "rainy"} ], [ {country: "georgia", value: "sunny"}, {country: "france", value: "gloomy"} ], [ {country: "georgia", value: "rainy"}, {country: "france", value: "dry"} ] ] var res = dictionary_demo.reduce(function(acc,e){ return acc.concat(e); },[]).reduce(function(acc, e){ if(!acc[e.country]) acc[e.country] = {[e.value]: 1}; else acc[e.country][e.value] = (acc[e.country][e.value] || 0) + 1; return acc; }, {}); console.log(res)Dado que se trata de una matriz de matrices, puede aplanar esas matrices con depth = 2 y luego puede usar la función Array.prototype.reduce para generar el resultado deseado.
const arr = [ [ {country: "georgia", value: "sunny"}, {country: "france", value: "rainy"} ], [ {country: "georgia", value: "sunny"}, {country: "france", value: "gloomy"} ], [ {country: "georgia", value: "rainy"}, {country: "france", value: "dry"} ]]; const result = arr.flat(2).reduce((a, {country, value}) => { const current = (a[country] ?? (a[country] = {[value]: 0})); a[country][value] = (a[country][value] ?? 0) + 1; return a; }, {}); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; }