VUE3中对数组和多重数组的合并,交集,并集

简单数组的合并

请添加图片描述
请添加图片描述

<template>
  <div>
    <div>数组1:{{ arry1 }}</div>
    <div>数组2:{{ arry2 }}</div>
    <el-button @click="Cli_union(0)">合并数组</el-button>
    <el-button @click="Cli_union(1)">交集数组</el-button>
    <el-button @click="Cli_union(2)">并集数组</el-button>
  </div>
</template>

<script setup>
let arry1 = [11, 24, 53, 56, 57, 88];
let arry2 = [56, 56, 77, 45, 26, 62, 88];

const union = [...new Set([...arry1, ...arry2])];
const cross = [...new Set(arry1.filter((it) => arry2.includes(it)))];
const diff = union.filter((it) => !cross.includes(it));

//合并数组
function Cli_union(res) {
  if (res == 0) {
    console.log("合并数组", union);
  } else if (res == 1) {
    console.log("数组的交集", cross);
  } else {
    console.log("数组的并集", diff);
  }
}
</script>

多重数组的合并

请添加图片描述

<template>
  <div>
    <div>数组1:{{ arry1 }}</div>
    <div>数组2:{{ arry2 }}</div>
    <div>合并的数组:{{ sumarry }}</div>
    <el-button @click="Btn_CLick">合并数组</el-button>
  </div>
</template>

<script setup>
let arry1 = [1, [2, [3, [4, [5]]]]];
let arry2 = [6, 7, 8, [9, [10]]];
function margeArrays(arrys) {
  let query = [...arrys];
  let result = [];
  while (query.length) {
    const item = query.shift(); 
    if (Array.isArray(item)) {
      query.unshift(...item); 
    } else {
      result.push(item);
    }
  }
  return result;
}

let sumarry = ref();
function Btn_CLick() {
  sumarry.value = margeArrays(arry1).concat(margeArrays(arry2));
  console.log(margeArrays(arry1).concat(margeArrays(arry2)));
}
</script>

小知识

shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
isArray() 方法用于判断一个对象是否为数组。 如果对象是数组返回 true,否则返回 false。
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3数组的使用和Vue2基本上没有变化。你可以使用常规的JavaScript方法来操作数组,例如push、pop、shift、unshift、splice等。但是Vue3提供了一些新的API和语法糖来更方便地操作数组。 1. reactive API Vue3的reactive API可以让任何JavaScript对象变成响应式对象。这意味着,当对象的属性发生改变时,Vue会自动重新渲染相关的视图。这个API同样适用于数组,因此你可以使用它来创建响应式的数组。 ```javascript import { reactive } from 'vue' const state = reactive({ todos: ['learn Vue', 'build app'] }) state.todos.push('deploy app') ``` 2. toRefs toRefs是一个将响应式对象转换成普通对象的API。它将对象的所有属性都转换成ref对象,这样可以方便地在模板使用。 ```javascript import { reactive, toRefs } from 'vue' const state = reactive({ todos: ['learn Vue', 'build app'] }) const refs = toRefs(state) console.log(refs.todos.value) // ['learn Vue', 'build app'] ``` 3. computed computed是Vue3用来创建计算属性的API。它可以接收一个getter函数和一个可选的setter函数。getter函数返回一个计算结果,setter函数用来更新计算结果。 ```javascript import { reactive, computed } from 'vue' const state = reactive({ todos: ['learn Vue', 'build app'] }) const todoCount = computed(() => { return state.todos.length }) console.log(todoCount.value) // 2 ``` 4. watchEffect watchEffect是一个API,它可以让你监听响应式数据的变化,并在变化时执行一个函数。它类似于Vue2的watch,但更加简洁。 ```javascript import { reactive, watchEffect } from 'vue' const state = reactive({ todos: ['learn Vue', 'build app'] }) watchEffect(() => { console.log('todos changed', state.todos) }) ``` 以上是Vue3操作数组的一些API和语法糖,它们可以让你更加方便地创建响应式的数组,并在模板使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值