zoukankan      html  css  js  c++  java
  • 遇到的数组问题

    一、数组的那些事

    1、js去除空对象

    方法
     
    	isEmpty(obj) {
    	   let empty = true;
    	   for (const key in obj) {
    	     if (obj[key]) {
    	        empty = false;
    	        break;
    	      }
    	    }
    	    return empty;
    	 }
    	 filter(array) {
    	    return array.filter(item => !this.isEmpty(item));
    	 }
      

    实例

    实例
     
    	isEmpty(obj) {
    	   let empty = true;
    	   for (const key in obj) {
    	     if (obj[key]) {
    	        empty = false;
    	        break;
    	      }
    	    }
    	    return empty;
    	 }
    	 filter(array) {
    	    return array.filter(item => !this.isEmpty(item));
    	 }
    	created() {
          this.InitLoad();
        }
        // 初始化数据
        InitLoad() {
          const oldArray = [
            {
              Code: "11",
              Name: "易烊千玺"
            },
            {
              Code: "12",
              Name: "王俊凯"
            },
            {
              Code: "12",
              Name: "王源"
            },
            {
              Code: "1",
              Name: "四叶草"
            },
            {
              Code: "",
              Name: ""
            }
          ];
          console.log(oldArray);
          const newArray = this.filter(oldArray);
          console.log(newArray);
        }
      

    2、数组去重

    (1)、方法1

    removalData(arrData) {
        const hash = {};
        arrData= arrData.reduce(function(item, next) {
                //id是你要以什么属性去重
            hash[next.id] ? '' : hash[next.id] = true && item.push(next);
            return item
        }, [])
        return arrData;
    }
    

    (2)、方法2

    newArray.filter(item => !oldArray.some(ele=>ele.id===item.id));
    

    3、ElementUI中获取select的label值

    <el-select v-model="value" @change="changeLbale(value)">
    	<el-option 
    		v-for="item in options" 
    	    :label="item.label" 
            :key="item.value" 
            :value="item.value">
    	</el-option>
    </el-select>
    value = "";
    getName = "";
    options = [
    	{
    		value: "选项1",
    		label: "黄金糕"
    	}
    ]
    changeLbale(val){
    	let obj = {};
    	obj = this.options.find((item) => {
    		return item.value === val;
    	})
    	this.getName = obj.label;
    }
  • 相关阅读:
    python学习---字符编码
    python学习--变量
    python学习--pycharm编辑器及优化设置
    python学习--第一个python程序helloworld
    python安装
    python学习---python2与python3的区别
    win7下weblogic安装与部署项目调试记录
    ubuntu获得root用户权限,使用xshell连接!
    linux命令
    nginx + tomcat 集群记录
  • 原文地址:https://www.cnblogs.com/DCL1314/p/11691161.html
Copyright © 2011-2022 走看看