gpt4 book ai didi

javascript - 如何将多个对象合并为一个?

转载 作者:行者123 更新时间:2023-12-02 21:23:40 24 4
gpt4 key购买 nike

当我进行http请求时,我正在获取对象数组,例如:

 [ 
{ row: '3', col: '1', val: '10:00' },
{ row: '3', col: '2', val: 'Some text' },
{ row: '3', col: '3', val: 'Yura' },
{ row: '3', col: '4', val: '676031383' },

{ row: '3', col: '6', val: '9:00 ' },
{ row: '3', col: '7', val: 'Some text' },
{ row: '3', col: '8', val: 'Alex' },
{ row: '3', col: '9', val: '965773775' },

{ row: '4', col: '1', val: '7:00' },
{ row: '4', col: '2', val: 'Some text' },
{ row: '4', col: '3', val: 'Anya' },
{ row: '4', col: '4', val: '951018689' },

{ row: '4', col: '6', val: '08:00' },
{ row: '4', col: '7', val: 'Some text' },
{ row: '4', col: '8', val: 'Dima' },
{ row: '4', col: '9', val: '961452584' },
]

问题是我需要将这些对象合并为一个,并添加一个键 - 类型,例如:

{ time: 10:00, info: 'Some text', name: 'Yura', phone: '676031383', type: 'Driver' }

{ time: 9:00, info: 'Some text', name: 'Alex', phone: '965773775', type: 'Passenger' }

{ time: 7:00, info: 'Some text', name: 'Anya', phone: '951018689', type: 'Driver' }

{ time: 8:00, info: 'Some text', name: 'Dima', phone: '951018689', type: 'Passenger' }

14类型应为“Driver”,从69应为“Passenger” ”。响应没有 5 列,因为她是空的

有可能以某种方式做到这一点吗?我花了几个小时,但没有结果

最佳答案

我想,这就是你一直在追求的:

const src = [{row:'3',col:'1',val:'10:00'},{row:'3',col:'2',val:'Some text'},{row:'3',col:'3',val:'Yura'},{row:'3',col:'4',val:'676031383'},{row:'3',col:'6',val:'9:00 '},{row:'3',col:'7',val:'Some text'},{row:'3',col:'8',val:'Alex'},{row:'3',col:'9',val:'965773775'},{row:'4',col:'1',val:'7:00'},{row:'4',col:'2',val:'Some text'},{row:'4',col:'3',val:'Anya'},{row:'4',col:'4',val:'951018689'},{row:'4',col:'6',val:'08:00'},{row:'4',col:'7',val:'Some text'},{row:'4',col:'8',val:'Dima'},{row:'4',col:'9',val:'961452584'},],

res = [...src]
.reduceRight((r,i,_,s) => (r.push(s.splice(0,4)),r),[])
.map((e,i) => ({time: e[0].val, info: e[1].val, name:e[2].val, phone: e[3].val, type: i&1 ? 'Passenger' : 'Driver'}))

console.log(res)
.as-console-wrapper{min-height:100%;}

关于javascript - 如何将多个对象合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60793059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com