'How to map `Object.values()` over an array of objects nesting objects?

I'm trying to map Object.values() to extract just the values from an array of objects:

const countries = [
    {
        "austria": { value: 1},
        "canada": {value: 2},
        "nepal": { value: 3},
        "india": { value: 4}
    },
    {
        "australia": { value: 10},
        "france": { value: 20},
        "egypt": { value: 40},
        "usa": { value: 20}
    },
    {
        "mexico": { value: 3},
        "germany": { value: 100},
        "southAfrica": { value: 1},
        "china": { value: 10}
    }
]

To get:

// expected output
[
    {
        "austria": [ 1 ],
        "canada": [ 2 ],
        "nepal": [ 3 ],
        "india": [ 4 ]
    },
    {
        "australia": [ 10 ] ,
        "france": [ 20 ],
        "egypt": [ 40 ],
        "usa": [ 20 ]
    },
    {
        "mexico": [ 3 ],
        "germany": [ 100 ],
        "southAfrica": [ 1 ],
        "china": [ 10 ]
    }
]

I tried

Object.values(countries) // returns the same as `countries`

and also tried mapping using ramda's map() to map Object.values() over countries

const R = require("ramda")

R.map(Object.values, countries)
// gives:
// [
//   [ { value: 1 }, { value: 2 }, { value: 3 }, { value: 4 } ],
//   [ { value: 10 }, { value: 20 }, { value: 40 }, { value: 20 } ],
//   [ { value: 3 }, { value: 100 }, { value: 1 }, { value: 10 } ]
// ]

But I don't want to lose the countries names! How can I utilize either vanilla map or ramda's version to get the expected output?



Solution 1:[1]

You need to map the entries and the nested objects values.

const
    countries = [{ austria: { value: 1 }, canada: { value: 2 }, nepal: { value: 3 }, india: { value: 4 } }, { australia: { value: 10 }, france: { value: 20 }, egypt: { value: 40 }, usa: { value: 20 } }, { mexico: { value: 3 }, germany: { value: 100 }, southAfrica: { value: 1 }, china: { value: 10 } }],
    result = countries.map(o => Object.fromEntries(Object
        .entries(o)
        .map(([k, v]) => [k, Object.values(v)]))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nina Scholz