es6中对数组的常用操作方法-对象数组

     //定义对象数组
      const arrayObject= [
        { name: 'name1', title: 'title1' },
        { name: 'name2', title: 'title2' },
        { name: 'name3', title: 'title3' }
      ];
     //数组对象
      //1、forEach
      arrayObject.forEach((item, index) => {
        console.log('item.name', item.name);
        console.log('item.title', item.title);
        console.log('index', index);
      });

      //2、 map
      const mapArray = arrayObject.map(item => {
        return item.title + '-map';
      });
      console.log('mapArray', mapArray);

      //3、reduce
      //将对象中的title属性的值挨个连起来
      const reduceResult = arrayObject.reduce(
        (prev, cur) => {
          console.log('prev', prev);
          console.log('cur', cur);
          return {title:prev.title +'-'+cur.title};
        }
      );
      console.log('reduceResult', reduceResult);

      //4、 filter
      const filterArray = arrayObject.filter(item => {
        return item.title === 'title1';
      });
      console.log('filterArray', filterArray);

      //5、every
      const everyResult = arrayObject.every(item => {
        return item.title === 'title1';
      });
      console.log('everyResult', everyResult);

      //6、some
      const someResult = arrayObject.some(item => {
        return item.title === 'title1';
      });
      console.log('someResult', someResult);

 

posted on 2024-07-26 18:11  技术高超  阅读(7)  评论(0编辑  收藏  举报