ゼウス:
どのように私はこの配列は、配列内の値に基づいて、新しいネストされた配列に変換することができます
CURRENT ARRAY
[{
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'
}]
このネストされた配列をMAKEするために探して
[{
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'
}]
}]
すべてのヘルプやヒントをいただければ幸いです。
palaѕn:
あなたの期待される結果は有効なものではありません。私が思うにcomment
、ここのようなオブジェクトの配列である必要があります:
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; }