gpt4 book ai didi

javascript - 将数组转换为带有数组的对象

转载 作者:行者123 更新时间:2023-11-30 19:36:04 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用一些数据创建导航菜单。为此,我需要将数据操作成一个数组对象。我正在尝试使用 map() 来做到这一点,我已经到了制作对象键和相应值的地步,但是,我不知道如何处理多个标题在它对应的年份。任何帮助将不胜感激。

const data = [
{
"fields": {
"title": "Frozen Thorns",
"year": 2017,
}
},
{
"fields": {
"title": "The Professional Years",
"year": 2018,
}
},
{
"fields": {
"title": "Green Nothing",
"year": 2018,
}
},
{
"fields": {
"title": "The Next Voyage",
"year": 2018,
}
},
{
"fields": {
"title": "Smooth Sorcerer",
"year": 2019,
}
},
{
"fields": {
"title": "Azure Star",
"year": 2019,
}
}]

const menu = Object.assign({}, ...data.map(item => ({[item.fields.year]: item.fields.title})));


// OUTPUT

// {
// 2017: "Frozen Thorns",
// 2018: "The Next Voyage",
// 2019: "Azure Star"
// }

// DESIRED OUTPUT

// {
// 2017: ["Frozen Thorns"],
// 2018: ["The Professional Years", "Green Nothing", "The Next Voyage"],
// 2019: ["Smooth Sorcerer", "Azure Star"]
// }

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