signed

QiShunwang

“诚信为本、客户至上”

new Set实现数组去重(根据名称重分数组)

2021/4/26 16:22:43   来源:

使用的数据

 "typeDevice" : [ {
	  "value_info" : 2,
      "info_name" : "123",
    }, {
      "value_info" : 2,
      "info_name" : "123",
    }, {
      "value_info" : 3,
      "info_name" : "33131",
    }, {
      "value_info" : 5,
      "info_name" : "123",
    } ]
			byqinfo() {
				//获取全部数据
				this.$http.get('/typeDevice/info', {
				}).then(res => {
					if (res.data.result === 1) {
						let server = res.data.data;
						server.map(item => {
							this.typeDevice = item.typeDevice
						})

						this.typeDevice.map(item => {
							return item.info_name
						})
						//根据名称分类
						if (this.typeDevice) {
							let keys = this.typeDevice.map(item => {
								return item.info_name
							})
							//new Set实现数组去重
							keys = [...new Set(keys)]
							let newArr = []
							
							//循环去重后的数组
							keys.forEach(item => {
								let obj = []
								//使用filter函数筛选
								obj = this.typeDevice.filter(val => {
									return item == val.info_name
								})
								//赋值数组
								obj = {
									...{
										key: item
									},
									...{
										vals: obj
									}
								}
								//添加数据到newArr里
								newArr.push(obj)
							})
						}
				})
			},