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

Javascript 从具有自定义键值的对象数组创建对象,javascript,arrays,object,Javascript,Arrays,Object,现在我知道有几个问题类似于和,但都没有回答我的问题 好吧,那么 假设我有一个api调用,我得到这样的响应 [ { amount: 23, bill: 47, otherData: null, title: 'cool title' }, { amount: 223, bill: 427, otherData: null, title: 'cooler title' }, { amount

现在我知道有几个问题类似于和,但都没有回答我的问题

好吧,那么

假设我有一个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'
  }
]
是否有一种方法可以从该数组创建新对象,并使用对象中的属性自定义键名??因此,所需的输出是

{
  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'
  }
}
现在我知道我可以将一组对象转换成这样的对象

var result = {};
for (var i=0; i<array.length; i++) {
  result[array[i].key] = array[i].value;
}
但我不知道如何从每个对象中获取标题,然后创建自定义键和对象

我甚至不确定这样的事情是否可行,任何帮助都将不胜感激


感谢

获取属性名称,提取标题并将其空格字符替换为大写字符。然后,它就简单地简化为一个对象:

const-input=[{amount:23,bill:47,otherData:null,title:'cool title'},{amount:223,bill:427,otherData:null,title:'cool title'},{amount:2313,bill:437,otherData:null,title:'super cool title'},{amount:123,bill:147,otherData:null,title:'cool title'}] console.log input.reducea,项=>{ 常量{title}=项; const camel=title.replace//g,chars=>chars[1]。toUpperCase; a[骆驼]=物品; 返回a; }, {} ; 使用map&reduce,reduce方法将返回带有自定义键的新对象。在reduce方法中使用map。这将创建一个title值数组,如['cool','title']。在同一映射方法中创建一个字符串 将单词的第一个字符转换为大写,并连接以连接所有单词

设oldArr=[{ 金额:23, 条例草案:47, 其他数据:null, 标题:“酷标题” }, { 金额:223, 条例草案:427, 其他数据:null, 标题:“酷标题” }, { 金额:2313, 条例草案:437, 其他数据:null, 标题:“超级酷标题” }, { 金额:123, 条例草案:147, 其他数据:null, 标题:“最酷的标题” } ] 设newArray=oldArr.reducefunctionacc,curr{ 让crtTitle=curr.title.split“”。mapfunctionitem,索引{ 如果索引!==0{ 返回item.substring0,1.toUpperCase+item.substring1,item.length; }否则{ 退货项目; } }.加入; acc[crtTitle]=当前; 返回acc }, {}; console.lognewArray