vue中实现合并两个有序数组
时间: 2023-12-18 10:27:22 浏览: 89
以下是Vue中实现合并两个有序数组的代码示例:
```vue
<template>
<div>
<p>合并前的两个有序数组:</p>
<p>{{ arr1 }}</p>
<p>{{ arr2 }}</p>
<p>合并后的有序数组:</p>
<p>{{ mergeArr }}</p>
</div>
</template>
<script>
export default {
data() {
return {
arr1: [1, 3, 5, 7, 9],
arr2: [2, 4, 6, 8, 10],
mergeArr: []
};
},
mounted() {
this.mergeArr = this.merge(this.arr1, this.arr2);
},
methods: {
merge(arr1, arr2) {
let i = 0,
j = 0,
k = 0;
let mergeArr = [];
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
mergeArr[k++] = arr1[i++];
} else {
mergeArr[k++] = arr2[j++];
}
}
while (i < arr1.length) {
mergeArr[k++] = arr1[i++];
}
while (j < arr2.length) {
mergeArr[k++] = arr2[j++];
}
return mergeArr;
}
}
};
</script>
```
阅读全文