简体   繁体   English

如何将具有包含值、值的对象的数组转换为只有键、值的单个对象?

[英]How to convert an array with objects that contain value,values to a single object with only key,values?

So after drawing data for the database I ended up with something like所以在为数据库绘制数据后,我最终得到了类似的东西

[
{name: "id", data_type: "integer"}
{name: "name", data_type: "char"}
{name: "active", data_type: "boolean"}
]

but what I want is:但我想要的是:

{id: "integer" , name: "char" , active: "boolean" }

I tried a bunch of things but couldn't figured it out, any helps would be appreciated我尝试了很多东西,但无法弄清楚,任何帮助将不胜感激

This is my desperate attempt:这是我绝望的尝试:

Object.entries(jsonData).map((type) => {
        return { ...objectList, [type[1].name]: type[1].data_type };
      }).reduce((objectList, object) => (
          ...objectList,
          object)
        )

Here is a simple approach:这是一个简单的方法:

 let arr = [ {name: "id", data_type: "integer"}, {name: "name", data_type: "char"}, {name: "active", data_type: "boolean"} ]; let map = {}; arr.forEach(e => map[e.name]=e.data_type); console.log(map);

If you want to use .reduce , try this:如果你想使用.reduce ,试试这个:

 let arr = [ {name: "id", data_type: "integer"}, {name: "name", data_type: "char"}, {name: "active", data_type: "boolean"} ]; let map = arr.reduce(function(obj,item){ obj[item.name] = item.data_type; return obj; }, {}); console.log(map);

Here is a reduce这是一个减少

 const data = [ {name: "id", data_type: "integer"}, {name: "name", data_type: "char"}, {name: "active", data_type: "boolean"} ]; const arrayToObject = arr => arr.reduce((result, value) => { result[value.name] = value.data_type; return result; }, {}); console.log(arrayToObject(data));

another way is using Array.reduce()另一种方法是使用Array.reduce()

const array = [
  { name: 'id', data_type: 'integer' },
  { name: 'name', data_type: 'char' },
  { name: 'active', data_type: 'boolean' },
];

const newObject = array.reduce((finalObj, obj) => {
  finalObj[obj.name] = obj.data_type;
  return finalObj;
}, {})

console.log(newObject)

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

相关问题 如何将 object 键值对以数组为值转换为键值对的多个对象? - How to convert object key value pair with array as values to multi objects of key value pair? 如何发送仅包含值的对象键值对? - How to send object key value pairs that contain values only? 将具有键值的对象转换为具有所述键名和值的对象数组 - Convert an object with key values to an array of objects with said key name and values 如何将对象数组转换为仅由值组成的 object - How to convert array of objects to object made up of only values 如何将单个键值对对象转换为具有键值对的对象数组? - How to convert a single key value pair object to array of objects with key value pair? 将键,值对象转换为仅值数组 - Transform a key, value object to values only array 如何将键值对象数组转换为具有单个属性的对象数组? - How to convert array of key–value objects to array of objects with a single property? 如何将具有数组作为值的对象转换为对象数组 - How to convert an object with arrays as values to an array of objects 在JavaScript中将值数组转换为对象(键值)对数组 - Convert array of values to array of objects (key-value) pairs in JavaScript 使用对象键值对的数组值将数组转换为对象 - Convert array to object using array values for object key value pairs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM