JS提升:JS中的数组扁平化问题

122 篇文章 11 订阅
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值