遇到的数组问题

一、数组的那些事

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;
}
posted @ 2019-10-17 11:50  不完美的完美  阅读(240)  评论(0编辑  收藏  举报