gpt4 book ai didi

arrays - typescript 将数组的特定属性连接为字符串

转载 作者:行者123 更新时间:2023-12-04 01:59:52 26 4
gpt4 key购买 nike

我有一个 API,它给了我这样的响应:

{
"item": [
{
"_id": "5a48e0c100a5863454c0af2a",
"name": "Maths",
"created_by": "5a43ee3231ad5a6b0850d961",
"__v": 0,
"created_on": "2017-12-31T13:06:09.957Z",
"active": 1,
"grade": [],
"syllabus": [
{
"_id": "5a47a5faed12d92d0c2449f4",
"name": "CBSE",
"description": "CBSE Syllabus",
"created_by": "5a43ee3231ad5a6b0850d961",
"__v": 0,
"created_on": "2017-12-30T14:43:06.305Z",
"banner": 1,
"active": 1
},
{
"_id": "5a47a615ed12d92d0c2449f5",
"name": "State Board",
"description": "State Board Syllabus",
"created_by": "5a43ee3231ad5a6b0850d961",
"__v": 0,
"created_on": "2017-12-30T14:43:33.328Z",
"banner": 1,
"active": 1
}
]
}
]
}

如您所见,在 maths 数组中有一个名为 item 的项目。现在在数学里面,我们有另一个 syllabus 数组。我想加入 maths 的所有教学大纲的名称。简单来说,说我想做这样的事情:
array element 1 - maths - CBSE, State Board
array element 2 - chemistry - CBSE, State Board

AFAIK,我们不能没有 2 forEach 循环。有没有更好的方法来处理这种情况?

最佳答案

大概是这样的:

  const result = item.map(it => ({
name: it.name,
syllabi: it.syllabus.map(s => s.name).join(', ')
}));

https://jsfiddle.net/ernmtcgj/

关于arrays - typescript 将数组的特定属性连接为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48052100/

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