美文网首页
vue项目-数组处理

vue项目-数组处理

作者: 樊小勇 | 来源:发表于2019-03-25 16:48 被阅读0次

在vue项目实战

  • 显示|隐藏
  • 查询返回数组
  • 过滤数组
  • 数组去重
  • 渲染列表
<template>
  <div>
    <h3>影院模块</h3>
    <button @click="showArea=!showArea">全城</button>
    <div v-show="showArea">
      <span @click="changeArea('all')">全部</span>
      <template v-for="item in areaList">
        <span @click="changeArea(item.districtId)" class="area" :key="item.districtId">{{item.name}}</span>
      </template>
    </div>

    <ul>
      <template v-for="(item,index) in cinemas">
        <li :key="index">{{item.name}}</li>
      </template>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 所有的影院,不能修改
      allCinemas: [],
      showArea: false,
      // 影院列表
      cinemas: [],
      // 区域列表
      areaList: []
    };
  },
  created() {
    this.getList();
  },

  methods: {
    async getList() {
      const url = "/cinema/getList";
      const res = await this.$http.get(url);
      this.allCinemas = res.cinemas;
      this.cinemas = res.cinemas;
      // 把影院数组里的district对象放进数组districts里
      const districts = res.cinemas.map(item => {
        return item.district;
      });
      // 去重
      this.areaList = this.quchong(districts);
    },

    /**
     * 去重的方法
     */
    quchong(districts) {
      // 定义一个新的数组,用来存放不重复的对象
      const list = [];
      districts.forEach(district => {
        // 到数组list里去查找有没有跟当前district对象一样的
        let index = list.findIndex(item => {
          return item.name === district.name;
        });
        // index为-1时,说明list没有跟当前区域对象一样的
        if (index === -1) {
          list.push(district);
        }
      });
      return list;
    },

    // 显示或隐藏区域列表
    // showAreaList() {
    //   this.showArea = !this.showArea;
    // },

    changeArea(districtId) {
      this.showArea = false;
      // 点击全部的时候,districtId为undefined
      if (districtId === "all") {
        this.cinemas = this.allCinemas;
      } else {
        // 根据id去过滤影院列表
        this.cinemas = this.allCinemas.filter(cinema => {
          return cinema.district.districtId === districtId;
        });
      }
    }
  }
};
</script>

<style scoped>
.area {
  background: green;
  margin-left: 10px;
  margin-top: 20px;
  cursor: pointer;
}
</style>

思路总结:在没有掌握全部技术的前提下怎么学习新的技术并且运用
思路:
了解需求(要达到什么效果)->根据自己现有的技能进行操作->碰到问题百度->看别人的解决方法->运用到自己的项目里
这个过程就是学习,而当问题解决了之后,可以看看有没有其他的方法,进行学习,这就是拓展
解决完问题用自己的话进行概述并表达出来,这便是总结
碰到问题第一得清醒,然后先把需求分析清楚,列出来,再挨个解决

相关文章

  • vue项目-数组处理

    在vue项目实战 显示|隐藏 查询返回数组 过滤数组 数组去重 渲染列表 思路总结:在没有掌握全部技术的前提下怎么...

  • 数组处理--使用vue处理

    数组追加 首端添加 插入到中间某位置 删除某位置元素 替换数组某元素 过滤器 数据转换 聚合函数

  • vue项目中碰到的问题

    vue项目中碰到的问题 路由懒加载写法: Vue 数组/对象更新 视图不更新深拷贝对象或者数组,视图会进行更新对象...

  • 2018-07-03

    关于 js 的数组操作 vue 项目中_this.compList.push(compList)_this.com...

  • __ob__: Observer

    在vue项目中输出到控制台出现,,数组元素中出现这个 [{…}, {…}, {…}, {…}, {…}, {…},...

  • vue数组响应式原理

    vue2中Object.defineProperty响应式只对对象有效,对数组无效,所以对数组做额外处理。我们知道...

  • 2018-09-11

    Vue中的for循环 v-for 指令可以绑定数组的数据来渲染一个项目列表: 链接Vue.js 输入的Vue.js...

  • 面试题整理(三)

    1.实现一个简单的双向绑定2.VUE对于数组不能更新问题的处理、defineProperty的缺陷?3.VUE为什...

  • uniapp和Android原生数据交互

    一、在uniapp项目添加一个类,专门用来处理和原生进行交互使用; import Vue from 'vue'; ...

  • 搭建项目架构

    项目准备 使用Vue CLI创建项目 加入Git管理 进行初始化文件处理删除src/assets/logo.png...

网友评论

      本文标题:vue项目-数组处理

      本文链接:https://www.haomeiwen.com/subject/lsjbvqtx.html