JS 数组对象中过滤重复数组

news/2024/7/19 13:33:48 标签: js

在一个复杂的数组对象数据中,有时候去过滤掉重复的数组,在网上搜到的,感觉挺好用的,也没有多深入研究,直接拿来用了(捂脸)

function deteleObject(obj) {
	var uniques = [];
	var stringify = {};
	for (var i = 0; i < obj.length; i++) {
		var keys = Object.keys(obj[i]);
		keys.sort(function(a, b) {
			return (Number(a) - Number(b));
		});
		var str = '';
		for (var j = 0; j < keys.length; j++) {
			str += JSON.stringify(keys[j]);
			str += JSON.stringify(obj[i][keys[j]]);
		}
		if (!stringify.hasOwnProperty(str)) {
			uniques.push(obj[i]);
			stringify[str] = true;
		}
	}
	uniques = uniques;
	return uniques;
}

方法使用:
①、先定义一个数组

let arr = [
	{
		key: 1,
		value: 'a',
		children: null
	},
	{
		key: 2,
		value: 'b',
		children: null
	},
	{
		key: 3,
		value: 'c',
		children: null
	},

	{
		key: 2,
		value: 'b',
		children: null
	},
	{
		key: 4,
		value: 'b',
		children: null
	},
	{
		key: 3,
		value: 'c',
		children: null
	}
]

②、直接调用去重方法
在这里插入图片描述
需要注意的是数组去重,就是对象中所有字段的值重复一样才可以去重掉,有一项不同则不可以,如下:
在这里插入图片描述


http://www.niftyadmin.cn/n/1770329.html

相关文章

VueCli2 中Vant定制主题的配置

1、首先安装依赖 npm install -D less3 less-loader5注意这里less和less-loader的版本不能太高&#xff0c;太高会报错 2、在main.js中引入vant的less文件 import vant/lib/index.less;3、创建自定义主题变量文件less 如下比如&#xff0c;resetui.less: red: #07c160; blu…

cli4配置vue.config.js文件时常见的问题

问题一&#xff1a; 执行npm run build的时候 导致以上报错的原因是compression-webpack-plugin这个插件的版本过高&#xff1b; 解决方法是卸载掉高版本安装低版本&#xff0c;执行命令如下&#xff1a; npm uninstall compression-webpack-plugin npm install compression-…

vue 中h5页面自适应的实现

有时候我们为了前端页面可以匹配不同分辨率的屏幕&#xff0c;会使用rem来对页面进行布局&#xff0c;无论基础px值设置得多么好计算&#xff0c;在写rem数值的时候&#xff0c;都需要对设计图原始的px值进行一番换算。 当当当当&#xff0c;来介绍一下两个好朋友&#xff0c;…

vue 路径@

1、是webpack定义的别名&#xff0c;在js中可以使用&#xff0c;如下&#xff1a; import util from /assets/js/util注&#xff1a;import xxxxx是js语法&#xff0c;所以可以使用别名 2、这个是在webpack.base.config.js中配置的&#xff0c;如下&#xff1a; resolve: {e…

vue 父组件中调用子组件的方法

Vue项目中如何在父组件中直接调用子组件的方法&#xff1a; 方案一&#xff1a;通过ref直接调用子组件的方法&#xff1b; //父组件中<template><div><Button click"handleClick">点击调用子组件方法</Button><Child ref"child&qu…

Vue 子组件调用父组件的方法

Vue项目中如何在子组件中直接调用父组件的方法&#xff1a; 方法一&#xff1a; 直接在子组件中通过this.$parent.event来调用父组件中的方法 //父组件 <template><div><child></child></div> </template> <script>import child f…

vue 生成二维码

一、页面中生成二维码方法 ①、首先安装qrcodejs2插件 npm install qrcodejs2 --save注意&#xff1a;安装完成之后可以在main.js中全局配置&#xff0c;也可以在单个页面中去引用 ②、引入 1&#xff09;单页面中引入 import QRCode from qrcodejs2;2&#xff09;全局配置…

elementUI中 form表单自定义验证方法

一、在form表单中经常去验证一些内容&#xff0c;有的是只比较简单的一些提示&#xff0c;有的则是需要自定义&#xff0c;实现方法如下&#xff1a; <template> <el-form :model"ruleForm2" :rules"dataRule" ref"dataForm" keyup.en…