es6数组去重、数组中的对象去重 && 删除数组(按条件或指定具体元素 如:id)&& 筛选去掉没有子组件的父组件
// 数组去重
{
const arr = [1,2,3,4,1,23,5,2,3,5,6,7,8,undefined,null,null,undefined,true,false,true,‘中文‘,‘‘,‘‘,‘hello‘,‘中文‘];
// 第一种
const r = [...new Set(arr)];
console.log(r); // [1, 2, 3, 4, 23, 5, 6, 7, 8, undefined, null, true, false, "中文", "", "hello"] // 第二种
const R = arr.reduce((all,name) => all.includes(name) ? all :[...all,name],[]);
console.log(R); // [1, 2, 3, 4, 23, 5, 6, 7, 8, undefined, null, true, false, "中文", "", "hello"] // 第三种
const k = arr.filter((item, i) => i === arr.indexOf(item));
console.log(k); // [1, 2, 3, 4, 23, 5, 6, 7, 8, undefined, null, true, false, "中文", "", "hello"]
} // 统计每个元素在数组中出现的次数
{
const arr = [‘zhangsan‘, ‘lisi‘, ‘wangwu‘, ‘zhangsan‘, ‘lisi‘, ‘wangwu‘,‘zhaosi‘,‘liuneng‘];
const r = arr.reduce((all, name) =>{
if(name in all){
all[name]++
}else{
all[name] = 1;
}
return all;
},{});
console.log(r); // {zhangsan: 2, lisi: 2, wangwu: 2, zhaosi: 1, liuneng: 1}
} // 数组对象去重
{
const arr = [
{
name:‘zhangsan‘,
age:12
},
{
name:‘lisi‘,
age:14
},
{
name:‘zhangsan‘,
age:12
},
{
name:‘lisi‘,
age:14
},
{
name:‘zhangsan‘,
age:12
},
{
name:‘lisi‘,
age:14
},
{
name:‘zhangsan‘,
age:12
},
{
name:‘wangwu‘,
age:16
},
{
name:‘wagnwu‘,
age:16
},
{
name:‘lisi‘,
age:14
}, ] // 根据 age 去重
// 方法一
const age = ‘age‘;
const r = arr.reduce((all, next) => all.some((atom) => atom[age] == next[age]) ? all : [...all, next],[]);
console.log(r); }
//指定具体元素//删除id=5的
let arr = [
{ id: 1, name: '北京' },
{ id: 2, name: '上海' },
{ id: 3, name: '深圳' },
{ id: 4, name: '广州' },
{ id: 5, name: '青岛' }
]
arr.splice(arr.findIndex(item=>item.id==5),1)
//find方法用于查找第一个符合条件的数组成员,如果没有找到返回undefind
let target = arr.find((item,index) => {
return item.id ==1
})
console.log(target)
//删除id大于3的(只保留小于等于3)
let arr2 = arr.reduce((total, current) => {
current.id<= 3 && total.push(current);
return total;
}, []);
//筛选去掉没有子组件的父组件 (parentId为0代表是父组件)
let arr = [
{
name:'1',
id:1,
parentId:0
},{
name:'3',
id:3,
parentId:0
},{
name:'2',
id:2,
parentId:0
},{
name:'1333',
id:13,
parentId:1
},{
name:'2444',
id:24,
parentId:2
}]
arr = arr.filter((x, index, self)=>{
// x : 数组每一项的值
// index: 每一项的下标
// self: 当前数组
return x.parentId!=0 || self.some((atom) => atom['parentId'] == x['id'] )
})
es6数组去重、数组中的对象去重 && 删除数组(按条件或指定具体元素 如:id)&& 筛选去掉没有子组件的父组件的更多相关文章
- vuejs利用props,子组件修改父组件的数据,父组件修改子组件的的数据,数据类型为数组
博文参考 传送们点一点 父组件: <template> <div> <aa class="abc" v-model="test" ...
- Vue中利用$emit实现子组件向父组件通信
Vue中利用$emit实现子组件向父组件通信 父组件 <template> <div> <p>我是父组件</p> <child :isShow=& ...
- 九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法
一.概述 React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. [父子组件]:组件的相互调用中,我们把调用者称为父 ...
- 从给数组中的对象去重看Javascript中的reduce()
假设有这样一个数组: let person = [ {id: 0, name: "小明"}, {id: 1, name: "小张"}, {id: 2, name ...
- java 集合 HashSet 实现随机双色球 HashSet addAll() 实现去重后合并 HashSet对象去重 复写 HashCode()方法和equals方法 ArrayList去重
package com.swift.lianxi; import java.util.HashSet; import java.util.Random; /*训练知识点:HashSet 训练描述 双色 ...
- vue 解决双向绑定中 父组件传值给子组件后 父组件值也跟着变化的问题
说明: 近日开发中碰见一个很诡异的问题, 父组件动态的修改对象 data 中的值, 然后将这个对象 data 传给子组件, 子组件拿到后将 data 中的值 乘以 100 ,发现父组件中的值也跟着变 ...
- vue中父组件给子组件传值,子组件给父组件传值
1.父组件传给子组件 父元素中 子元素中(通过props传值) 2.子组件传给父组件 子元素中(this.$emit(传过去的名字,传的参数)) 父元素中 通过changeShow的参数data 把修 ...
- 关于Vue中,父组件获取子组件的数据(子组件调用父组件函数)的方法
1. 父组件调用子组件时,在调用处传给子组件一个方法 :on-update="updateData" 2. 子组件在props中,接收这个方法并声明 props: { onUp ...
- 【Vue项目笔记】—— 父子组件之间传递参数和子组件执行父组件中的方法
父组件(MyBlog.vue) <template> <!-- Delete Modal --> <!-- 注意:这里的@deleteBlog中的deleteBlog要和 ...
- 小姐姐手把手教你JS数组中的对象去重
有时候数据库中的数据重复的,我们另一个需求需要数据的唯一性 那么这时候就用到这个方法了 我还是以截图的方式发粗来 不然太丑了 见谅 console.log(map)打印出来的结果已经帮我们把需要的 ...
随机推荐
- LP1-5:测试设计
在接到产品需求进行开发前,怎么样才能最大程度的降低开发错误或明显bug的情况? 答案是在「开发前做设计」. 通常,一个功能的设计要包含几个方面: 1.已有功能情况 2.需求情况 3.数据库设计 4.接 ...
- Property or method "scope" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components
报错如下 属性或方法"范围"不是在实例上定义的,而是在呈现期间引用的. 通过初始化属性,确保此属性是反应性的,无论是在数据选项中,还是对于基于类的组件. 原因在template中未 ...
- docker tar包下载地址
https://download.docker.com/linux/static/stable/x86_64/
- 【Frida】Java反射调用
通过反射调用,获取类名 参数Object obj 类名 String str 方法名 static Object a(Object obj, String str) { try { return ob ...
- react toolkit 异步请求之后调取其他函数
在slice切片文件中,页面dispatch执行action之后,异步请求完成后调取另外一个异步请求,要在异步请求的 第二个参数添加 thunkAPI,调取thunkAPI的dispatch方法即 ...
- 性能测试-ps与vmstat
1.ps命令-获取当前系统的进程状态 ps >ps(process status) 获取帮助: man ps 获取当前系统的进程状态 ps-ef-eF-elv 使用标准语法查看系统上的每个进程 ...
- webpack和source map
当 webpack 打包源代码时,可能会很难追踪到 error(错误) 和 warning(警告) 在源代码中的原始位置. 如果打包后代码有一处错误,特别是使用的vue.react这些前端框架.打包后 ...
- 【GNU/Linux, Debian】使用cups连接HP Laserjet 1012 HB打印机
woc我怎么第一版打的cpus Cups是个开源的打印软件,于2007年被苹果收购(包括作者负责人也被苹果雇佣),收购后任然使用GPL发行. 然而2021年年底,作为收购前负责人的他表示这个软件再也不 ...
- pytest学习总结
官方pytest文档:Full pytest documentation - pytest documentation 一.pytest以及辅助插件的安装 1.pytest安装 pip install ...
- Oracle一次插入多条数据
Oracle一次插入多条数据(批量插入)语法:INSERT ALL INTO tableName (column1, column2, column_n) VALUES (expr1, expr2, ...