简体   繁体   English

Javascript 将对象列表转换为具有键值的 object

[英]Javascript transform list of objects into object with key value

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?在 JavaScript 中是否有任何简单的方法可以实现这一点?

Extract the values of each object in the array using Array.prototype.map , then construct an object from those entries using Object.fromEntries . 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript-将JavaScript对象数组转换为排序的键/值javascript对象 - Javascript - transform array of javascript objects into a sorted key/value javascript object 将 object 转换为单个键/值对象的数组 - Transform an object into an array of single key/value objects 转换/解析javascript对象键值对 - Transform/parse javascript object key value pairs 使用对象的属性作为键转换对象列表中的对象数组 - Transform an array of objects in an object list with a property of the object as key 将具有相同长度列表属性的 object 转换为 javascript 中的对象列表? - Transform an object with properties of same length list to a list of objects in javascript? 使用相同的键(将值作为数组)将多个对象转换为一个对象 - Transform multiple objects into one object with same key, value as an array 如何使用键值对在 javascript 中转换 object - how to transform object in javascript using key value pair 如何在javascript中将项目转换为键/值对对象? - How to transform an item into a key/value pair object in javascript? 在 JavaScript 中递归地将 JSON 对象转换为键/值数组 - Recursively transform a JSON object to an array of key/value in JavaScript 将字符串转换为对象键值 - Transform string to object key value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM