var mappings = [ { character: '1', symbol: '$' }, { character: 's', symbol: '@' } ]
I want to turn this into:
{
1: "$",
s: "@"
}
Is there any easy way to accomplish this in JavaScript?
Extract the values of each object in the array using Array.prototype.map
, then construct an object from those entries using Object.fromEntries
.
var mappings = [ { character: '1', symbol: '$' }, { character: 's', symbol: '@' } ] let result = Object.fromEntries(mappings.map(e => Object.values(e))) console.log(result)
var mappings = [ { character: '1', symbol: '$' }, { character: 's', symbol: '@' } ] const newMappings = mappings.map(item => ({[item.character]: item.symbol})) const reduceMapping = newMappings.reduce((acc, current) => ({...acc, ...current}),{}) console.log(reduceMapping)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.