当前位置:网站首页>JS promotion: array flattening in JS
JS promotion: array flattening in JS
2022-07-27 23:55: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 Do not use indexes directly covers[0], Please use concatAll, map, filter, forEach complete result The result is [ { 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" }, ] */
Try a solution :
/* 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)
边栏推荐
- Reinforcement learning - pytorch realizes advantage actor critical (A2C)
- JUC工具包学习
- 为什么 Redis 集群要使用反向代理? 看这篇就明白了
- The share price soared 180.46%! Shanghai silicon industry, the leader of domestic large silicon wafers, is listed: the cumulative net profit in recent four years is less than 60million
- What technology is RPA process automation robot? How to realize office automation?
- 76000 people shut down! Toshiba announced the closure of all factories in Japan
- CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
- The txt file named according to the sequence number is renamed from the back to the front
- 【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
- Apple releases new iPhone se: equipped with A13 bionic processor, priced from 3299 yuan
猜你喜欢

Latex中如何加粗字体 & 如何打出圆圈序号

为什么 Redis 集群要使用反向代理? 看这篇就明白了

重新定义分析 - EventBridge 实时事件分析平台发布

org.junit.runners.model.InvalidTestClassError: Invalid test class ‘com.zhj.esdemo.MysqlTests‘: 1.

尚硅谷尚品项目汇笔记(一)

BUUCTF-RSA4

File&递归14.1

资深如何确定软件测试结束的标准

Bank marketing predicts the success rate of a customer's purchase of financial products

数据管理的重点
随机推荐
Master data management theory and Practice
数据管理的重点
史上最简明的 Tcpdump 入门指南,看这一篇就够了
Smartrefresh nested multiple recycleview sliding conflicts and incomplete layout display
What technology is RPA process automation robot? How to realize office automation?
【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
Zabbix4.0 uses SNMP agent to monitor vcenter6.5
Elk log analysis system installation and deployment
基于原生js实现今日新闻网站
[ACTF新生赛2020]crypto-aes
Arm32 for remote debugging
面试官问线程安全的List,看完再也不怕了!
What is the prospect of low code development? Are you really optimistic about low code development?
BUUCTF-[BJDCTF2020]RSA1
Monologue of a software Investor: why don't I pursue fast-growing companies
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
2022/7/24-7/25
4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?
Edit the copy and paste judgment problem (bug?), WYSIWYG display symbol problem feedback.
BUUCTF-bbbbbbrsa