当前位置:网站首页>JS - array de duplication in the array object case
JS - array de duplication in the array object case
2022-06-13 08:22:00 【tingkeaiii】
One 、 subject

Two 、 Code and effect
<script>
// Method 1 :
var arr3 = ['c', 'a', 'z', 'a', 'x', 'a', 'x', 'c', 'b'];
var newArr2 = [];
// Traverse the old array , Compare it with the new array , The new array does not , Just save from the old array , If you have something, you won't save it .
for (let i = 0; i < arr3.length; i++) {
if (newArr2.indexOf(arr3[i]) === -1) { /* There are no original array elements in the new array , Just turn back -1*/
newArr2[newArr2.length] = arr3[i]; /* Save the non-existent array in the new array */
}
}
console.log(newArr2);
// Method 2:
var arr2 = ['c', 'a', 'z', 'a', 'x', 'a', 'x', 'c', 'b'];
for (var i = arr2.length; i >= 0; i--) { /*i In reverse order */
for (let j = 0; j < arr2.length; j++) { /*j positive sequence */
/* When the elements are the same but the index numbers are different */
if (arr2[i] === arr2[j] && arr2.indexOf(arr2[j]) !== arr2.lastIndexOf(arr2[i])) {
var temp = arr2[i];
arr2[i] = arr2[arr2.length - 1];
arr2[arr2.length - 1] = temp;/* Repeat the back one , Swap positions with the last element */
arr2.pop();/* Delete the last element */
}
}
}
console.log(arr2);
</script>
3、 ... and 、 Ideas
Method 1 :

Method 2 :


边栏推荐
- 淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口
- DNS domain name resolution service
- 微服务项目搭建三:自动生成代码
- Batch package and download Alibaba OSS files
- The method of SolidWorks modifying text font in engineering drawing
- Logstash failed to create queue
- LVM management exercise
- Cosmos Starport installation and startup
- Import the robot model built by SolidWorks into ROS
- Maternal and infant supplies wholesale industry uses management software to improve efficiency and realize cost reduction and efficiency increase
猜你喜欢

酒水批发行业应当如何高效管理商品与库存

How to efficiently manage commodities and inventory in the beverage wholesale industry

Overall process analysis of account book operation in fabric0.6

Practice makes sense -- your byte alignment and stack cognition may be wrong

Buuctf web (VI)

学习记录4: einops // cudnn.benchamark=true // hook

How to use annotations in word

Buffer Overflow Vulnerability Lab

Remote access and control

Founder of Starbucks: no longer open "public toilets" to non store consumers for safety reasons
随机推荐
AcWing 1977. Information relay (base ring tree, parallel search set)
Gtk+ programming example on page 115 - simplest progress bar 2 with steps to write GTK program using anjuta
Penetration problem (main directory, password explosion, database uploading Trojan horse)
适合生鲜批发行业的几种精准接单方式
Edge浏览器使用BdTab新标签页插件(BD新标签页)
Cosmos star module development
生鲜配送分拣管理系统哪家比较强?
Buuctf web (V)
Cosmos star application case
Buuctf web (VI)
Learning record 4:einops / / cudnn benchamark=true // hook
Determination of ranking
Process and scheduled task management
How can the small and medium-sized lighting industry make use of the digital transformation to stand out from the encirclement?
Logstash failed to create queue
CCNP_ BT static routing
Import the robot model built by SolidWorks into ROS
How app inventor accesses resource files in assets directory
中小型照明灯饰行业如何利用数字化转型突出重围?
Remote access and control
