当前位置:网站首页>JS提升:JS中的数组扁平化问题
JS提升:JS中的数组扁平化问题
2022-07-27 21:05:00 【The..Fuir】
var courseLists = [{ "name": "My Courses", "courses": [{ "id": 511019, "title": "React for Beginners", "covers": [{ width: 150, height: 200, url: "http://placeimg.com/150/200/tech" }, { width: 200, height: 200, url: "http://placeimg.com/200/200/tech" }, { width: 300, height: 200, url: "http://placeimg.com/300/200/tech" }], "tags": [{ id: 1, name: "JavaScript" }], "rating": 5 }, { "id": 511020, "title": "Front-End automat workflow", "covers": [{ width: 150, height: 200, url: "http://placeimg.com/150/200/arch" }, { width: 200, height: 200, url: "http://placeimg.com/200/200/arch" }, { width: 300, height: 200, url: "http://placeimg.com/300/200/arch" }], "tags": [{ "id": 2, "name": "gulp" }, { "id": 3, "name": "webpack" }], "rating": 5 }] }, { "name": "New Release", "courses": [{ "id": 511022, "title": "Vue2 for Beginners", "covers": [{ width: 150, height: 200, url: "http://placeimg.com/150/200/nature" }, { width: 200, height: 200, url: "http://placeimg.com/200/200/nature" }, { width: 300, height: 200, url: "http://placeimg.com/300/200/nature" }], "tags": [{ id: 1, name: "JavaScript" }], "rating": 5 }, { "id": 511023, "title": "Angular2 for Beginners", "covers": [{ width: 150, height: 200, url: "http://placeimg.com/150/200/people" }, { width: 200, height: 200, url: "http://placeimg.com/200/200/people" }, { width: 300, height: 200, url: "http://placeimg.com/300/200/people" }], "tags": [{ id: 1, name: "JavaScript" }], "rating": 5 }] }]; /* var result = courseList 不得直接使用索引 covers[0],请用 concatAll, map, filter, forEach 完成 result 結果为 [ { id: 511019, title: "React for Beginners", cover: "http://placeimg.com/150/200/tech" }, { id: 511020, title: "Front-End automat workflow", cover: "http://placeimg.com/150/200/arch" }, { id: 511022, title: "Vue2 for Beginners", cover: "http://placeimg.com/150/200/nature" }, { id: 511023, title: "Angular2 for Beginners", cover: "http://placeimg.com/150/200/people" }, ] */
尝试解决方法:
/* courseLists.forEach((firstValue) => { // console.log(firstValue.courses.values()); for (const iterator of firstValue.courses.values()) { var result = {} result.id = iterator.id; result.title = iterator.title; result.covers = iterator.covers; console.log(result); } }) */ /* let result = [] courseLists.forEach(item => { item.courses.forEach(item2 => { let obj = { id: item2.id, title: item2.title, cover: item2.covers[0].url } result.push(obj) }) }) console.log(result); */ /* courseLists.forEach((firstValue) => { // console.log(firstValue.courses.values()); for (const iterator of firstValue.courses.values()) { var result = {} result.id = iterator.id; result.title = iterator.title; iterator.covers.forEach((value)=>{ result.cover=value.url }) console.log(result); } }) */ /* let a=courseLists.map(({ courses }) => { return courses.map(({ id, title, covers }) => { return covers.map(({ url }) => ({ id, title, url, })); }); }).flat(3); console.log(a); */ /* let result=[] courseLists.forEach(courseLists=>{ courseLists.courses.forEach(course=>{ let obj={ id:course.id, title:course.title, cover:'' } for(let i=0;i<course.covers.length;i++){ let {url}=course.covers[i]; if(url.includes('150/200')){ obj.cover=url; break; } } result.push(obj); }) }) console.log(result); */ const newArr = courseLists .map(item => item.courses).flat(1) .map(outItem => outItem.covers .filter(item => item.width === 150 && item.height === 200) .map(item =>({ id: outItem.id, title: outItem.title, cover: item.url })) ).flat(1)
边栏推荐
- 你的列表很卡?这4个优化能让你的列表丝般顺滑
- NB-IoT产业的现状与未来:跨过1亿出货门槛,奔向5G大连接!
- Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
- Design and implementation of spark offline development framework
- My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline
- 加速IGBT国产化!比亚迪半导体将独立上市,市值或达300亿元!
- 详解分布式系统的幂等
- Zabbix4.0 uses SNMP agent to monitor vcenter6.5
- What are the advantages of Tita's OKR system over other similar products or shared documents?
- 一位软件投资者的独白:我为什么不追逐快速增长的公司
猜你喜欢

29.学习Highcharts 使用百分比的堆叠柱形图

五子棋人机对战实现

Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步

My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline

Nature review: preferential effects in the formation of microbial communities

Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server

【C语言】通讯录(动态版本)

Interviewer: let's talk about the specific process of network data transmission
![[elm classification] classification of UCI data sets based on nuclear limit learning machine and limit learning machine, with matlab code](/img/bd/fe105f0af3a24ad6f220fa35bb5e8d.png)
[elm classification] classification of UCI data sets based on nuclear limit learning machine and limit learning machine, with matlab code

西门子PLC能否实时无线采集多处从站模拟量数据?
随机推荐
Zabbix4.0使用SNMP代理方式监控vcenter6.5
Xu Jinbo: AI protein prediction and design
ELK日志分析系统安装和部署
NDK series (6): let's talk about the way and time to register JNI functions
技术认证 | 图扑软件携手华为云再创合作共赢新局面
Normality of confidence interval
Disable caching with meta HTML tags in all browsers
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
Binary conversion method
真的很难理解?RecyclerView 缓存机制到底是几级缓存?
Arm32进行远程调试
ZCMU--1720: 死亡如风,我要装逼
疫情之下,台积电一季度增长超预期,7nm占比35%!二季度或创新高
进制转换方法
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全
[signal denoising] signal denoising based on Kalman filter with matlab code
Date的使用
Lua基础语法学习
【CVA估值训练营】如何快速读懂上市公司年报——第四讲
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?