I have input list of object,
[
{"id":1,"name":"USD - US Dollar","country":"US","created_at":"2018-05-28
14:54:24","updated_at":"2018-05-28 14:54:24"},
{"id":2,"name":"TH- Thai Bat","country":"TH","created_at":"2018-05-28
14:54:24","updated_at":"2018-05-28 14:54:24"}
]
I want to transform into , anyone please guide me.
{"US": "USD - US Dollar","TH": "TH- Thai Bat"}
Use reduce
to turn an array into a single object:
const input = [
{"id":1,"name":"USD - US Dollar","country":"US","created_at":"2018-05-28 14:54:24","updated_at":"2018-05-28 14:54:24"},
{"id":2,"name":"TH- Thai Bat","country":"TH","created_at":"2018-05-28 14:54:24","updated_at":"2018-05-28 14:54:24"}
];
const output = input.reduce((a, { name, country }) => {
a[country] = name;
return a;
}, {});
console.log(output);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments