当前位置:网站首页>JS merge multiple string arrays to maintain the original order and remove duplicates
JS merge multiple string arrays to maintain the original order and remove duplicates
2022-06-13 02:52:00 【Programming Bruce Lee】
Realization effect
Merge any string array , And keep the original order of strings for merging and de duplication , The effect is shown in the figure :
The implementation code is as follows
First, simply implement the merging between two string arrays , recycling js The implementation of recursive call with variable parameters of n A combination of string arrays
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Dragon brother </title>
</head>
<body>
<script type="text/javascript"> /** * Merge in order n Array of strings and remove duplicates * @param {...any} strArrys // Array of multiple character strings */ function mergeStrArrays(...strArrys) {
if (!strArrys) {
return []; } // Recursive export if (strArrys.length < 2) {
return strArrys[0] ? strArrys[0] : []; } // Recursively call return mergeStrArrays(mergeStrArray(strArrys.pop(), strArrys.pop()), ...strArrys); } /** * Merge two string arrays in order and remove duplicates * @param {Array} strArry1 Array of strings * @param {Array} strArry2 Array of strings * @returns */ function mergeStrArray(strArry1, strArry2) {
if (strArry2.length > strArry1.length) {
// Guarantee strArry1 Than strArry2 Long let strT = strArry2; strArry2 = strArry1; strArry1 = strT; } for (let i = 0; i < strArry1.length; i++) {
if (strArry2[i] === undefined) {
break; } // If the corresponding position str1 Elements and str2 Elements are different and str1 There is no such element in the array , Add... At this location str2 Elements if (strArry1[i] !== strArry2[i]) {
let j = 0; for (j; j < strArry1.length; j++) {
if (strArry1[j] === strArry2[i]) {
break; } } if (j >= strArry1.length) {
strArry1.splice(i, 0, strArry2[i]); } } } return strArry1; } let str1 = ["aaa", "bbb", "ccc", "ddd", "eee"] let str2 = ["bbb", "ccc", "eee", "ffff"] let str3 = ["bbb", "ccc", "mmm", "ggg"] console.log(" The three string arrays are :"); console.log(str1); console.log(str2); console.log(str3); console.log(" After the merger :"); console.log(mergeStrArrays(str1, str2, str3)); </script>
</body>
</html>
边栏推荐
- 遍历数组,删除某元素,直到删除为止
- Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
- 04 route jump and carry parameters
- Hash tables: metaphrases
- The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- mysql索引
- [reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face
- Opencv 17 face recognition
- [reading papers] dcgan, the combination of generating countermeasure network and deep convolution
猜你喜欢

Introduction and download of common data sets for in-depth learning (with network disk link)

冲刺强基计划数学物理专题一
![[data and Analysis Visualization] data operation in D3 tutorial 3-d3](/img/c9/82385b125a8e47d900c320b5f4f3b2.jpg)
[data and Analysis Visualization] data operation in D3 tutorial 3-d3

OpenCVSharpSample04WinForms

Graduation project - campus old thing recycling system based on stm32
![[life science] DNA extraction of basic biological experiments](/img/84/c1968c2c08feab44b14a529420eea9.jpg)
[life science] DNA extraction of basic biological experiments

The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)

Binary tree initialization code

Matlab: find the inner angle of n-sided concave polygon
![[reading papers] visual convolution zfnet](/img/01/4181f19b2d24b842488522c2001970.jpg)
[reading papers] visual convolution zfnet
随机推荐
Binary tree initialization code
How to manage the IT R & D department?
House raiding
My practice of SOA architecture project based on WCF
Ijkplayer source code -- mnatemediaplayer of ijkmediaplayer
Change the topic of change tax
Digital IC Design -- FIFO design
Prometheus安装并注册服务
Model prediction of semantic segmentation
Pycharm and Anaconda ultra detailed installation and configuration tutorial
Retrofit easy to use
遍历数组,删除某元素,直到删除为止
Linked list: adding numbers in the linked list
PCR validation of basic biological experiments in [life sciences]
Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)
專業的數據庫管理軟件:Valentina Studio Pro for Mac
[data analysis and visualization] key points of data drawing 4- problems of pie chart
Introduction to facial expression recognition system - Technical Paper Edition
[data analysis and visualization] key points of data drawing 9- color selection
Opencv 9 resize size change rotate rotate blur mean (blur)