第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何從當(dāng)前數(shù)組創(chuàng)建嵌套數(shù)組?

如何從當(dāng)前數(shù)組創(chuàng)建嵌套數(shù)組?

溫溫醬 2022-06-09 19:46:42
如何根據(jù)數(shù)組中的值將此數(shù)組轉(zhuǎn)換為新的嵌套數(shù)組當(dāng)前陣列[{ name: 'John', review: 'Its great', book: 'Jungle Book'},{ name: 'Steve', review: 'Learned a lot', book: 'Coding Standards'},{ name: 'Chris', review: 'Love it', book: 'Coding Standards'},{ name: 'Jake', review: 'Would read again', book: 'Jungle Book'}]想做這個(gè)嵌套數(shù)組[{ book: 'Jungle Book', comment: [{   name: 'Jake',   review: 'Would read again'  },  {   name: 'John',   review: 'Its great'     }]},{ book: 'Coding Standards', comment: [{   name: 'Chris',   review: 'Love it'  },  {   name: 'Steve',   review: 'Learned a lot'     }]}]任何幫助或提示將不勝感激。
查看完整描述

2 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

您的預(yù)期結(jié)果不是有效的。我認(rèn)為comment這里需要一個(gè)對象數(shù)組,例如:


const temp = [{name:"John",review:"Its great",book:"Jungle Book"},{name:"Steve",review:"Learned a lot",book:"Coding Standards"},{name:"Chris",review:"Love it",book:"Coding Standards"},{name:"Jake",review:"Would read again",book:"Jungle Book"}];


const res = Object.values(temp.reduce((ac, { name, review, book }) => {

  ac[book] = ac[book] || {book, comment: []}

  ac[book].comment.push({name, review})

  return ac;

}, {}));


console.log(res);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復(fù) 2022-06-09
?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

const arr = [{

 name: 'John',

 review: 'Its great',

 book: 'Jungle Book'

},

{

 name: 'Steve',

 review: 'Learned a lot',

 book: 'Coding Standards'

},

{

 name: 'Chris',

 review: 'Love it',

 book: 'Coding Standards'

},

{

 name: 'Jake',

 review: 'Would read again',

 book: 'Jungle Book'

}];



const result = arr.reduce(function (r, a) {

    r[a.book] = r[a.book] || [];

    r[a.book].push(a);

    return r;

}, Object.create(null));


console.log(result);

你可以做我上面做的或者_.groupBy()從 lodash 使用。



查看完整回答
反對 回復(fù) 2022-06-09
  • 2 回答
  • 0 關(guān)注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號