亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將多個對象合二為一?

如何將多個對象合二為一?

開心每一天1111 2022-05-26 14:02:27
當我進行 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' },]問題是我需要將這些對象組合成一個并再添加一個 key- type,例如:{ 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' }From 1to 4type 應該是“Driver”,from 6to9應該是“Passenger”。響應沒有 5 列,因為她是空的有可能以某種方式做到這一點嗎?我花了幾個小時,但沒有結果
查看完整描述

2 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

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


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%;}


查看完整回答
反對 回復 2022-05-26
?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

我重新排列了輸出格式,因為您可以輕松獲取所需的數據進行操作。這里我附上代碼。如果不符合您的要求,請隨時發表評論。


const data = [ 

  { 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: '10:00' },

  { row: '4', col: '2', val: 'Some text' },

  { row: '4', col: '3', val: 'Yura' },

  { row: '4', col: '4', val: '676031383' },

  

  { row: '4', col: '6', val: '9:00 ' },

  { row: '4', col: '7', val: 'Some text' },

  { row: '4', col: '8', val: 'Alex' },

  { row: '4', col: '9', val: '965773775' },

];


const result = {}

data.forEach(v => result[v.row] = {driver: {}, passenger: {}});


data.forEach(value => {

  if (value.col === '1')      result[value.row].driver.time = value.val;

  else if (value.col === '2') result[value.row].driver.info = value.val;

  else if (value.col === '3') result[value.row].driver.name = value.val;

  else if (value.col === '4') result[value.row].driver.phone = value.val;

  else if (value.col === '6') result[value.row].passenger.time = value.val;

  else if (value.col === '7') result[value.row].passenger.info = value.val;

  else if (value.col === '8') result[value.row].passenger.name = value.val;

  else if (value.col === '9') result[value.row].passenger.phone = value.val;

});


console.log(result);


查看完整回答
反對 回復 2022-05-26
  • 2 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號