简体   繁体   English

从具有自定义键值的对象数组创建对象

[英]Create an object from an array of objects with custom key values

now I know there are a few questions that are similar like this question and this question but neither answer my question. 现在我知道有几个类似的问题,例如这个问题这个问题,但是都没有回答我的问题。

Okay so... 可以,然后呢...

Say I have an api call and I get a response like this 说我有一个api调用,并且得到了这样的响应

[
  {
     amount: 23,
     bill: 47,
     otherData: null,
     title: 'cool title'
  },
  {
     amount: 223,
     bill: 427,
     otherData: null,
     title: 'cooler title'
  },
  {
     amount: 2313,
     bill: 437,
     otherData: null,
     title: 'super cool title'
  },
  {
     amount: 123,
     bill: 147,
     otherData: null,
     title: 'coolest title'
  }
]

is there a way I can create a new object from this array and have custom key names using a property in the object?? 有没有办法我可以从该数组创建一个新对象,并使用该对象中的属性创建自定义键名? so the desired output is.. 所以所需的输出是..

{
  coolTitle: {
     amount: 23,
     bill: 47,
     otherData: null,
     title: 'cool title'
  },
  coolerTitle: {
     amount: 223,
     bill: 427,
     otherData: null,
     title: 'cooler title'
  },
  superCoolTitle: {
     amount: 2313,
     bill: 437,
     otherData: null,
     title: 'super cool title'
  },
  coolestTitle: {
     amount: 123,
     bill: 147,
     otherData: null,
     title: 'coolest title'
  }
}

now I know I can convert an array of objects into an object like so.. 现在我知道我可以将对象数组转换成这样的对象。

var result = {};
for (var i=0; i<array.length; i++) {
  result[array[i].key] = array[i].value;
}

but I have no Idea how I could get the title from each object, camelCase it and then create the custom key and the object 但我不知道如何从每个对象中获取标题,驼峰式命名,然后创建自定义键和对象

I'm not even sure if something like this is possible, any help would be appreciated 我什至不确定是否可以进行此类操作,我们将不胜感激

Thanks 谢谢

To get the property name, extract the title and replace its space-characters with the upper-case character. 要获取属性名称,请提取title并将其空格字符替换为大写字符。 Then, it's as simple as reduce -ing into an object: 然后,这就像reduce变成对象一样简单:

 const input=[{amount:23,bill:47,otherData:null,title:'cool title'},{amount:223,bill:427,otherData:null,title:'cooler title'},{amount:2313,bill:437,otherData:null,title:'super cool title'},{amount:123,bill:147,otherData:null,title:'coolest title'}] console.log( input.reduce((a, item) => { const { title } = item; const camel = title.replace(/ ./g, chars => chars[1].toUpperCase()); a[camel] = item; return a; }, {}) ); 

Use map & reduce , The reduce method will return the new object with custom key. 使用mapreduce ,reduce方法将使用自定义键返回新对象。 Inside the reduce method use map . 在reduce方法内部使用map This will create an array of the value of title like ['cool','title'] . 这将创建一个title值的数组,例如['cool','title'] Inside the same map method create a string to convert the first character of the word to upperCase and join to join all the words 在相同的map方法内,创建一个字符串,将单词的第一个字符转换为upperCase,并join以连接所有单词

 let oldArr = [{ amount: 23, bill: 47, otherData: null, title: 'cool title' }, { amount: 223, bill: 427, otherData: null, title: 'cooler title' }, { amount: 2313, bill: 437, otherData: null, title: 'super cool title' }, { amount: 123, bill: 147, otherData: null, title: 'coolest title' } ] let newArray = oldArr.reduce(function(acc, curr) { let crtTitle = curr.title.split(' ').map(function(item, index) { if (index !== 0) { return item.substring(0, 1).toUpperCase() + item.substring(1, item.length); } else { return item; } }).join(''); acc[crtTitle] = curr; return acc }, {}); console.log(newArray) 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM