当前位置:网站首页>Number array de duplication in JS
Number array de duplication in JS
2022-06-25 23:56:00 【wen_ Wen Wen】
// Method 1: ES6 Medium Set Data structure method
function arrRemoval(array) {
// let set = Array.from(new Set(array));
let set = [...new Set(array)];
return set;
}
// Method 2: Define an empty new array , Each time, judge whether the new array contains the current element , If not, add
function arrRemoval(array) {
let result = [];
array.forEach((ele,ind) =>{
if(!result.includes(ele)) {
result.push(ele);
}
})
return result;
}
// Method 3: indexOf Law , indexof(item): Returns the index of the first element found in the array
function arrRemoval(array) {
let result = [];
result = array.filter((ele,ind) =>{
return array.indexOf(ele)===ind;
})
return result;
}
// Method 4: reduce Law :
function arrRemoval(array) {
let result = [];
// Define the initial pre by []
result = array.reduce( function(pre, item){
// return pre.indexOf(item)!==-1 ? pre : pre.concat(item);
return pre.indexOf(item)!==-1 ? pre : [...pre,item];
}, [])
return result;
}
// Method 5: Leverage the key Non repeatable features , If key Repetition is set later value Cover the front of value
function arrRemoval(array) {
let result = {};
// Define the initial pre by []
array.forEach((ele,ind) => {
result[ele] = ind;
});
// console.log(Object.keys(result)); //["1", "2", "3", "4", "5"]
return Object.keys(result).map((ele,ind)=>{
// ~~ The returned result is numeric
// return ~~ele;
return parseInt(ele);
})
}
// Method 6: Prioritize , Then compare the i Xiang He i+1 term ,
function arrRemoval(array) {
let result = [];
array.sort();
for(let i=0;i<array.length;i++) {
if(array[i] !== array[i+1]) {
result.push(array[i]);
}
}
return result;
}
// Method 7: Compare the current element with each item after the array , If there are equality items, discard the current element , If there is no equality, it will be added to the new array
function arrRemoval(array) {
let result = [];
for(let i=0;i<array.length;i++) {
for(let j=i+1;j<array.length;j++) {
if(array[i] == array[j]) {
i++;
}
}
result.push(array[i]);
}
return result;
}
边栏推荐
- 社交网络可视化第三方库igraph的安装
- step7和wincc联合仿真_过路老熊_新浪博客
- The role of iomanip header file in actual combat
- 【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)
- IDEA中如何生成get/set方法
- Bit Compressor [蓝桥杯题目训练]
- unsigned与signed之大白话
- php socket通信中stream_select方法的理解
- Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
- 树莓派开机发送热点进行远程登录
猜你喜欢

iomanip头文件在实战中的作用

final和static

6.常用指令(上)v-cloak,v-once,v-pre

【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)

Uniapp -- the use of document collation and push of unipush

6. common instructions (upper) v-cloak, v-once, v-pre

兆欧表电压档位选择_过路老熊_新浪博客

SSM integrated learning notes (mainly ideas)

Connecting MySQL database with VBScript_ Old bear passing by_ Sina blog

使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容
随机推荐
Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案
idea 查看单元测试覆盖率
产品经理如何把控产品开发的进度
(转载)进程和线程的形象解释
JS中常用知识点
keil编译运行错误,缺少error:#5:#includecore_cm3.h_过路老熊_新浪博客
用ES5的方式实现const
Gradle的环境安装与配置
JS中的数字数组去重
PHP interprocess pass file descriptor
Use Baidu map API to set an overlay (infowindow) in the map to customize the window content
如何进行流程创新,以最经济的方式提升产品体验?
Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
MySQL InnoDB锁知识点
谈一谈生产环境中swoole协程创建数量控制机制
Format the number. If the number is not enough, fill in 0, for example, 1:0001,25:0025
登录拦截器
Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog
实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式