如何使用键值对转换javascript中的对象

如何使用键值对转换javascript中的对象,javascript,arrays,object,arrayobject,Javascript,Arrays,Object,Arrayobject,我需要一些操作对象的帮助 我所拥有的: actions: [ { action_type: 'comment', value: '1' }, { action_type: 'link_click', value: '5' }, { action_type: 'post_reaction', value: '1' }, { action_type: 'landing_page_view', value: '5' }, 我需要的是: actions: [

我需要一些操作对象的帮助

我所拥有的:

 actions: [
    { action_type: 'comment', value: '1' },
    { action_type: 'link_click', value: '5' },
    { action_type: 'post_reaction', value: '1' },
    { action_type: 'landing_page_view', value: '5' },
我需要的是:

actions: [
    { comment : 1 },
    { link_click : 5 },
    { post_reaction : 1 },
    { landing_page_view : 5 },

我怎样才能做到这一点? 提前感谢您的帮助!:

const newActions=actions.mapitem=>{[item.action\u type]:item.value}
如果道具名称不会更改,则可以使用array.map,array.map通过将每个元素转换为新形式来创建新数组:

actions.mapa=>{[a.action\u type]:a.value} 对象的道具可以通过像obj[myKey]这样的字符串索引来访问,因此您可以使用action[i]。action\u type作为新对象中的键。

您可以使用map

常量动作=[ {操作类型:'comment',值:'1'}, {action_type:'link_click',value:'5'}, {action_type:'post_reaction',值:'1'}, {action_type:'landing_page_view',值:'5'} ] console.logactions.mapitem=>{[item.action\u type]:item.value}